Click here to Skip to main content
13,898,344 members
Rate this:
Please Sign up or sign in to vote.
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)

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);
                        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.
Updated 16-Apr-18 17:16pm
F-ES Sitecore 17-Apr-18 4:07am
You seem to be closing the client connection after you call startClient?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190306.1 | Last Updated 17 Apr 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100