Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I have created a client and a server howerver whenever I run multiple clients and then close 1 of them the other clients disconnect as soon as they try to message other active clients. This may be due to a mistake inside my code however I still cant manage to find where the problem lies.

Whenever I close a client I call this code:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  Socket_Client.Close();
}


Below is a link to another post where I have displayed the entire code which I currently use. I have created a post on this site giving more information regarding my problem. Any help will be greatly appreciated.

c# - Disconnecting from server close all other connections when I try to send message to a connected user - Stack Overflow[^]

What I have tried:

I have tried to locate the problem and found that withing the method
private void doChat()


Both the method :
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

                        dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                        Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
                        rCount = Convert.ToString(requestCount);
                        //ConnUsers.Add(clNo);
                        Program.broadcast(dataFromClient, clNo, true);


And the catch method is Fired.

The output I get on server which is a console based application is for example

The server is listening
client1 joined
client2 joined
client3 joined
client1 disconnected
client2 says hey
client2 disconnected

As the example displayed once 1 client is disconnected it seems to close all the clients whenever I try to send a message to the connected clients.

Any help will be greatly appreciated. I have been stuck on this problem for quite some time as I am new to coding with server / client application.
Posted
Updated 16-Apr-18 17:16pm
Comments
F-ES Sitecore 17-Apr-18 4:07am    
You seem to be closing the client connection after you call startClient?

1 solution

I didn't go through all the code but looking at the exception handling in doChat it looks like when an exception occurs you assume that the client has disconnected. However, there are tons of reasons why an exception might occur.

So my suggestion is, modify the exception handling so that you first handle the exceptions that you're prepared to. After that handle other exceptions by logging or otherwise collecting and publish information about the exception. Console.WriteLine is one possibility or you could use a log file if you like.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900