TcpClient - An existing connection was forcibly closed by the remote host

后端 未结 3 1272
一生所求
一生所求 2021-01-05 19:06

The Info

I have been developing a web http server in c# and decided to add a remote console feature. The console can be used from any location and u

3条回答
  •  甜味超标
    2021-01-05 19:45

    I don't know if you fixed your issue or not but I guess you should post your workaround at least so others can check it.

    I don't fully understand your issue but I had the same exception, but mine was triggered while the client disconnected and server was trying to read the stream (networkStream).

    I had a single command for reading

    networkstream.Read(mybuffer, 0, mybuffer.length);
    

    As the checked answer suggested I changed that for:

    do
    {
     byte[] buff = new byte[1];
     networkstream.Read(buff, 0, 1);
     myreceivedbuff.Add(buff);
    } while (networkstream.DataAvailable)
    

    this also produced the issue while client disc, so I had to do this

    do
    {
     byte[] buff = new byte[1];
     try
     {
      networkstream.Read(buff, 0, 1);
     }
     catch(exception ex)
     {
      throw new exception("The dam client disconnected in the middle of a transaction.");
     }
     myreceivedbuff.Add(buff);
    } while (networksteam.DataAvailable)
    

    I had to do this since it doesn't matter if its on a client or a server the exception is the same. host disconnection meanwhile my exception was CLIENT disconnection and this generic message misguided me to the solution.

    Sorry if the code is not pasted from vs but I typed here so fix the capitalization so it can compile.

    Hope this helps someone.

提交回复
热议问题