question

AlexeyShakin-8378 avatar image
0 Votes"
AlexeyShakin-8378 asked AlexeyShakin-8378 commented

NetworkStream freezes infinitely

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.




dotnet-csharpwindows-platform-network
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The code you've shared is incomplete. The community needs all the relevant code to find the mistakes. For example, where is the code that listens for a connection? Or listens for a response? The code that you have shown blocks which I think is the problem but we do not have all the code.

Reference documentation
Asynchronous Client Socket Example
Asynchronous Server Socket Example

Synchronous Client Socket Example
Synchronous Server Socket Example


0 Votes 0 ·

1 Answer

AlexeyShakin-8378 avatar image
0 Votes"
AlexeyShakin-8378 answered AlexeyShakin-8378 commented

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);


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What do you mean - "blocks". Could you elaborate a little?

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT AlexeyShakin-8378 ·

@AlexeyShakin-8378, I also tested the code you provided, Please refer to the link Sending and Receiving XML data over TCP to check if it works for you.


0 Votes 0 ·