开发者

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:

Network stream problem

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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜