1

I have used other apps with each the listener and the client and the same thing happens the listener's stream.read() only reads once and client's stream.write() only writes once.

here's the Listener's loop:

while (true)
        {
            Console.Write("Waiting for a connection... ");

            // Perform a blocking call to accept requests.

            TcpClient client = server.AcceptTcpClient();
            Console.WriteLine("Connected!");

            data = null;

            // Get a stream object for reading and writing
            NetworkStream stream = client.GetStream();

            int i;

            // Loop to receive all the data sent by the client.
            while ((i = stream.Read(bytes, 0, bytes.length)) != 0)
            {
                // Translate data bytes to a ASCII string.
                data = Encoding.ASCII.GetString(bytes, 0, i);
                Console.WriteLine("Received: {0}", data);

            }

            // Shutdown and end connection
            client.Close();
        }

after the first iteration it just blocks and doesn't receive anything even when the connection is lost it keeps blocking, however after some time has passed it gives me this exception:

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

same exception on the second stream.write() in the Client's side:

System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

I think this might be a firewall problem and if so, can someone tell me why it happens and how to fix it ? Thank you in advance.

Edit: stream.DataAvailable is false after the first stream.read() so it's not a duplicate of What are some reasons NetworkStream.Read would hang/block?

Note: the Listener's code and the Client's were both taken from https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener?view=netframework-4.7.2#examples and https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient?view=netframework-4.7.2#examples

  • Remove the Close() : client.Close(); – jdweng Nov 06 '18 at 18:35
  • client.Close() is not even reached – Rami Alwaheeb Nov 06 '18 at 18:40
  • ARE YOU SURE? Youi are getting the write error because the connection was closed, not due to a firewall!!! – jdweng Nov 06 '18 at 18:47
  • for client.Close() to be reached, stream.read() must return zero, and it only does that when the end of the stream has been reached – Rami Alwaheeb Nov 06 '18 at 18:58
  • Are you constantly sending data forever? When you get to the end of the stream you exit the loop and close? – jdweng Nov 06 '18 at 19:17
  • if connection closes after one stream.write() then yes, but that doesn't make any sense and I don't think this is what's happening, also I ran the Listener without client.close() and the problem persists. – Rami Alwaheeb Nov 06 '18 at 19:27
  • The client also has same issue with closing after one message!!!!!! – jdweng Nov 06 '18 at 20:46
  • There is no "end of the stream" with network streams. You typically follow a protocol, and determine how client and server will communicate using that protocol. Crudely, you can send the size of the message as the first X bytes of a conversation. – Kenneth K. Nov 06 '18 at 20:47
  • Possible duplicate of [What are some reasons NetworkStream.Read would hang/block?](https://stackoverflow.com/questions/6958255/what-are-some-reasons-networkstream-read-would-hang-block) – Kenneth K. Nov 06 '18 at 20:50
  • @KennethK. I saw that question and my problem is not in read() itself it's that the stream just closes on it's own or something like that I'm not sure – Rami Alwaheeb Nov 06 '18 at 23:10

0 Answers0