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

后端 未结 3 1185
一生所求
一生所求 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:30

    It seems that your client closes the connection after one message.

    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
    client.Close();
    return responseData; //returns what server writes
    

    If you want to persist the connection you should have a loop on the client similar to the one you have on the server. If you want to establish a new connection every time you should close the stream gracefully on the server and not have a loop like this. You will still need to loop in case the message is longer or you need to specify max length for the command.

    0 讨论(0)
  • 2021-01-05 19:37

    I had same solution. it is usually happens if client is disconnected. Solution from Alex RG is not working unfortunately. you will get another exception. Best solutions is described here by Microsoft

    you need to check using CanRead

    TcpClient tcpClient = new TcpClient ();
    
    // Uses the GetStream public method to return the NetworkStream.
    NetworkStream netStream = tcpClient.GetStream ();
    
    if (netStream.CanRead)
    {
        // Reads NetworkStream into a byte buffer.
        byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
    
        // Read can return anything from 0 to numBytesToRead. 
        // This method blocks until at least one byte is read.
        netStream.Read (bytes, 0, (int)tcpClient.ReceiveBufferSize);
    
        // Returns the data received from the host to the console.
        string returndata = Encoding.UTF8.GetString (bytes);
    
        Console.WriteLine ("This is what the host returned to you: " + returndata);
    
    }
    else
    {
        Console.WriteLine ("You cannot read data from this stream.");
        tcpClient.Close ();
    
        // Closing the tcpClient instance does not close the network stream.
        netStream.Close ();
        return;
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题