Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 7th, 2008, 08:35 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default Can't intialize a Hastable in Constructor

Hi

I am not sure what is going I have 2 hashtables both private statics that orignally where above the constructor and an object was made for each on.

So it was like this

public static Hashtable users = new Hashtable(30);
public static Hashtable connections = new Hashtable(30);
public contstructor()
{
}

So the hashtable was set to 30 so I could have 30 users and 30 connections.

Now I want to change it so the user can change how many users they can have to connect to the chat program. So what I did was I made a textbox and through the form(since this code is in a file called server.cs) I sent through the number and I was planning to set it to whatever number they choose.

Now here is the problem it does not work. I send it through the constructor and the users hashTable gets set to the correct number. I then try to set the connection hashTable to the same number and it just ignores it and sets it to zero.

I don't understand why like there both declared the same way.


Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;




namespace Comp7615_Assignment1.App_Code.Assignment2.Server
{

    public delegate void StatusChangedHandler(object sender, StatusChangedEventArgs e);

    class ChatServer
    {

        private IPAddress ip;
        private TcpClient tcpClient;
        private Thread thrListener;
        private TcpListener tlsClient;
        private int portNum;
        private bool ServiceRunning = false;
        private static StatusChangedEventArgs e;
        public static event StatusChangedHandler StatusChanged;
        public static Hashtable users;
        public static Hashtable connections;


        public ChatServer(IPAddress ip, int portNum,int numberOfUsers)
        {
            this.ip = ip;
            this.portNum = portNum;
            users = new Hashtable(numberOfUsers); // this will get set so if 30 was the hashtable will get to 30.
            connections = new Hashtable(numberOfUsers); // this will sort of set since it goes from null to 0 but not what I want.

        }


        public static void AddUser(TcpClient tcpUser, string username)
        {

            users.Add(username, tcpUser);
            connections.Add(tcpUser, username);

            SendAdminMessage(connections[tcpUser] + " has joined us");
        }


        public static void RemoveUser(TcpClient tcpUser)
        {

            if (connections[tcpUser] != null)
            {

                SendAdminMessage(connections[tcpUser] + " has left us");


                ChatServer.users.Remove(ChatServer.connections[tcpUser]);
                ChatServer.connections.Remove(tcpUser);
            }
        }


        public static void OnStatusChanged(StatusChangedEventArgs e)
        {
            StatusChangedHandler statusHandler = StatusChanged;
            if (statusHandler != null)
            {

                statusHandler(null, e);
            }
        }

        public static void SendAdminMessage(string message)
        {
            StreamWriter swSenderSender;

            e = new StatusChangedEventArgs("Administrator: " + message);
            OnStatusChanged(e);

 
            TcpClient[] tcpClients = new TcpClient[ChatServer.users.Count];

            ChatServer.users.Values.CopyTo(tcpClients, 0);

            for (int i = 0; i < tcpClients.Length; i++)
            {

                try
                {

                    if (message.Trim() == "" || tcpClients[i] == null)
                    {
                        continue;
                    }

                    swSenderSender = new StreamWriter(tcpClients[i].GetStream());
                    swSenderSender.WriteLine("Administrator: " + message);
                    swSenderSender.Flush();
                    swSenderSender = null;
                }
                catch 
                {
                    RemoveUser(tcpClients[i]);
                }
            }
        }


        public static void SendMessage(string from, string message)
        {
            StreamWriter swSenderSender;

            e = new StatusChangedEventArgs(from + " says: " + message);
            OnStatusChanged(e);


            TcpClient[] tcpClients = new TcpClient[ChatServer.users.Count];

            ChatServer.users.Values.CopyTo(tcpClients, 0);

            for (int i = 0; i < tcpClients.Length; i++)
            {

                try
                {

                    if (message.Trim() == "" || tcpClients[i] == null)
                    {
                        continue;
                    }

                    swSenderSender = new StreamWriter(tcpClients[i].GetStream());
                    swSenderSender.WriteLine(from + " says: " + message);
                    swSenderSender.Flush();
                    swSenderSender = null;
                }
                catch 
                {
                    RemoveUser(tcpClients[i]);
                }
            }
        }

        public void StartListening()
        {

            tlsClient = new TcpListener(ip,portNum);



            tlsClient.Start();


            ServiceRunning = true;


            thrListener = new Thread(KeepCheckingForConnections);
            thrListener.Start();
        }

        private void KeepCheckingForConnections()
        {

            while (ServiceRunning == true)
            {

                tcpClient = tlsClient.AcceptTcpClient();

                Connection newConnection = new Connection(tcpClient);
            }
        }
    }
}
 
Old October 8th, 2008, 01:01 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I think you are misunderstanding the concept of the 'capacity' of a hashtable. Just because you set its initial capacity to 30 does not mean that you cannot add more than 30 items, or that it will have 30 items in it - just that its internal structures will be ready when you want to add the first 30, and it will have to do a 'reshuffle' when you add more.

If you want a fixed limit then you will have to implement your own limits. Also I would recommend using a generic Dictionary<> instead of 2 hashtables.

/- Sam Judson : Wrox Technical Editor -/
 
Old October 8th, 2008, 02:08 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Ya I just found that out. I though the limit was like that but then again that's how the tutorial I was following led me to believe.

 
Quote:
quote:The 30 defines how many users the chat server can hold at one given point, but you can easily go into the hundreds if needed,without worrying about a performance decrease.


Anyways I just removed the 30 since if it does not limit anything I don't think I need it. Still don't understand why the constructor would not set it.

generic Dictionary<> I will try that another time I don't really feel like going back and changing everything around.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on hastable code petfrog BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 February 22nd, 2008 05:15 PM
Constructor nayeem69 .NET Framework 2.0 1 July 17th, 2007 12:33 AM
private constructor in C# n.nsivakumar C# 1 August 16th, 2006 07:08 AM
static constructor sreenu.pocha C# 1 July 26th, 2006 07:21 AM
Constructor ramess C++ Programming 1 March 18th, 2006 07:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.