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);
}
}
}
}