public static Hashtable clientsList = new Hashtable();
private void Form1_Load(object sender, EventArgs e)
{
}
public IPAddress ip;
public string GetIp()
{
string hostname = Dns.GetHostName();
IPHostEntry ipentry = Dns.GetHostEntry(hostname);
IPAddress[] addr = ipentry.AddressList;
return addr[addr.Length - 1].ToString();
}
public void func()
{
// richTextBox3.Text = richTextBox3.Text + "\nServer is online now!";
richTextBox3.Invoke(new Action(() => richTextBox3.Text = richTextBox3.Text + "\nServer is online now!"));
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
// richTextBox1.Text = richTextBox1.Text + "\n" + ("Chat Server Started ....");
richTextBox1.Invoke(new Action(() => richTextBox1.Text = richTextBox1.Text + "\n" + ("Chat Server Started ....")));
counter = 0;
while ((true))
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
clientsList.Add(dataFromClient, clientSocket);
broadcast(dataFromClient + " Joined ", dataFromClient, false);
//richTextBox1.Text = richTextBox1.Text +"\n"+ (dataFromClient + " Joined chat room ");
richTextBox1.Invoke(new Action(() => richTextBox1.Text = richTextBox1.Text + "\n" + (dataFromClient + " Joined chat room ")));
startClient(clientSocket, dataFromClient, clientsList);
}
clientSocket.Close();
serverSocket.Stop();
//richTextBox1.Text = richTextBox1.Text + "\n" + "exit";
richTextBox1.Invoke(new Action(() => richTextBox1.Text = richTextBox1.Text + "\n" + "exit"));
Console.ReadLine();
}
public void broadcast(string msg, string uName, bool flag)
{
foreach (DictionaryEntry Item in clientsList)
{
TcpClient broadcastSocket;
broadcastSocket = (TcpClient)Item.Value;
NetworkStream broadcastStream = broadcastSocket.GetStream();
Byte[] broadcastBytes = null;
if (flag == true)
{
broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg);
}
else
{
broadcastBytes = Encoding.ASCII.GetBytes(msg);
}
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
broadcastStream.Flush();
}
} //end broadcast function
public TcpClient clientSocket;
public string clNo;
public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
{
Hashtable clientsList;
this.clientSocket = inClientSocket;
this.clNo = clineNo;
clientsList = cList;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = 0;
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
// richTextBox1.Text = richTextBox1.Text + "\n" + ("From client - " + clNo + " : " + dataFromClient);
richTextBox1.Invoke(new Action(() => richTextBox1.Text = richTextBox1.Text + "\n" + ("From client - " + clNo + " : " + dataFromClient)));
rCount = Convert.ToString(requestCount);
broadcast(dataFromClient, clNo, true);
}
catch (Exception ex)
{
MessageBox.Show("Test");
//richTextBox1.Text = richTextBox1.Text + "\n" + (ex.ToString());
richTextBox1.Invoke(new Action(() => richTextBox1.Text = richTextBox1.Text + "\n" + (ex.ToString())));
}
}//end while
}//end doChat
private void button1_Click(object sender, EventArgs e)
{
func();
}
According to me error is occuring in bold marked line! What to do to resolve this problem?? Thanks in advance...