NetworkStream freezes infinitely

Alexey Shakin 1 Reputation point
2022-04-11T10:31:41.24+00:00

Hello All!
I write a client - server utility which sends a xml document through network and does some work on the server side with it. I use a NetworkStream object as a parameter for "Save" and "Load" methods of a XDocument object.

on the client side:

public static void StartClient(XDocument NewInit)
        {
              TcpClient tcpClient = new TcpClient();
              NetworkStream stream = tcpClient.GetStream();
              NewInit.Save(stream);
         }

on the server side:

NetworkStream stream = tcpClient.GetStream();
XDocument NewInit = XDocument.Load(stream);

It works fine. But I need to do some work with the received document and send an answer back to the client. I try to do this using "Write" metod of the strim.

On the server side, immediately after the strings above:

        byte[] msg = Encoding.ASCII.GetBytes("Object created!");
        stream.Write(msg, 0, msg.Length);

On the client side, immediately after saving xml into the stream:

             byte[] bytes = new byte[1024];
              int bytesRec = stream.Read(bytes, 0, bytes.Length);
            textBox.Text = Encoding.ASCII.GetString(bytes, 0, bytesRec);

That is where my program freezes. And not only the client does not get the answer, but the xml is not being received either.
Please, tell me how to do it right.

Windows for business | Windows Client for IT Pros | Networking | Network connectivity and file sharing
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Alexey Shakin 1 Reputation point
    2022-04-14T12:31:01.697+00:00

    I tried to resolve the problem using the references in the comment, but with no result. So here is the original code.

    server

         IPAddress ipAddress = IPAddress.Any;
                 IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
                TcpListener Listener = new TcpListener(localEndPoint);
                Listener.Start();
                Console.WriteLine("Waiting for a connection...");
                TcpClient tcpClient = Listener.AcceptTcpClient();
                NetworkStream stream = tcpClient.GetStream();
                XDocument xmlNewInit = XDocument.Load(stream);
     // Send back a response.
                byte[] msg = Encoding.ASCII.GetBytes("Object created!");
                stream.Write(msg, 0, msg.Length);
    

    client

    IPAddress ipAddress = IPAddress.Parse("192.168.1.49");
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
    
                    // Create a TCP/IP  socket.  
    TcpClient tcpClient = new TcpClient();
    
    
                    // Connect the socket to the remote endpoint. Catch any errors.  
    
    tcpClient.Connect(remoteEP);
    textBox.Text = "Socket connected to " + remoteEP.ToString();
    
     NetworkStream stream = tcpClient.GetStream();
    XElement NewInit = new XElement(.....)
    NewInit.Save(stream);
    // Receive the response from the remote device. 
    int bytesRec = stream.Read(bytes, 0, bytes.Length);
    textBox.Text = Encoding.ASCII.GetString(bytes, 0, bytesRec);
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.