Network stream problem
I have a server with multiple threads. Here is my server connection:
while (true) {
client = this.tcpListener.AcceptTcpClient();
sThread a = new sThread(form1, listaThreads);
lock(Program.lockThreads) {
listaThreads.Add(a);
}
Thread clientThread =
new Thread(new ParameterizedThreadStart(a.HandleClientComm));
clientThread.Start(client);
}
In my sThread class I have the following code:
public void HandleClientComm(object client)
{
String a = "";
try // nu uita sa pui inapoi!
{
tcpClient = (Tcp开发者_如何学GoClient) client;
clientStream = tcpClient.GetStream();
sr = new StreamReader(clientStream);
sw = new StreamWriter(clientStream);
a = sr.ReadLine();
...
But in some cases I get an error at a = sr.ReadLine();
that says the following:
What can it be?
Sometimes remote hosts reboot, or the user kills the client program, or routers reboot losing their state and drop all the connections that they used to carry.
Handling client disconnects is just part of programming reliable software, and you should handle this System.Net.Sockets.SocketException
by cleaning up whatever state you have stored for the client and moving on.
Of course, if you also wrote the client software and your users say it is giving similar error messages, then you should investigate further. :)
精彩评论