'Httpwebrequest(client writemode) to TCPclient(Server Readmode) Approach

I am working on sending data across the network with the traditional server-client model.

Here Server starts the Tcplistener in a particular address and port. In this case, it is the local host.

The client makes use of WebRequest class in .net and takes the request stream and starts writing data into the request stream.

Let me walk through the Server code class:

TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"),9309);
tcpListener.Start()//Start the listener and wait for client to connect.
while(true)
{
 TcpClient newclient = tcpListener.AcceptTcpClient();
 if(newclient.Connected())
     {
     break;
     }
}
while(true)
{
 
ReadData(newclient);
}
public void ReadData(TcpClient newclient)
{
            byte[] buffer = newbyte[50];
            Stream ns = newclient.GetStream();     
            ns.Read(buffer, 0, buffer.Length);
            Console.WriteLine( Encoding.UTF8.GetString(buffer));
 }

//End of Server class.

Now let's see the Client Code class:-

         WebRequest Request = HttpWebRequest.Create(http://127.0.0.1:9309/DataChannel);
         Request.Method = "POST";  
         //Below method registers to Server's AcceptTcpClient  and tcpclient is assigned.     
         Stream NetworkStream = ModifyCollimationRequest.GetRequestStream();
      int DataWritten = 0;
        while(true)
          {
            string Dname = "\r\nPosting server with Data as {0}\r\n";
            byte[] dbytes = Encoding.UTF8.GetBytes(string.Format(Dname, ++DataWritten));
            ns.WriteAsync(dbytes, 0, dbytes.Length);
            ns.FlushAsync();
          }
//End of Client code. Once the connection is established, the client keeps writing into the stream till the buffer size of >65000 is reached without issue but the problem is with the Server.

In Server, Stream ns = newclient.GetStream(); -> This line under ReadData() method of the server executes, but the the next line the code where Read() is used -> the code does not throw exception nor reaches next line. It just exits while debugging or times out. Someone it feels like, I am not able to fetch the stream or stream is empty. But the client keeps writing without any issue. Can sometimes try this out and help me with what I am missing. Ultimately, I should be able to read the data available in a stream in any case but not sure why. Please add in your suggestions?



Solution 1:[1]

At the end of the client code, we need to call the below method for the data to start actual streaming

ResponseStream = (HttpWebResponse)Request.GetResponse(); 

This is because there seems to be a bug in this .NET API where the buffered data is sent only when GetResponse() stream is called. This fixed the issue for me.

Below StackOverflow URL helped me learn more on this problem HttpWebRequest.GetRequestStream returned stream does not send data immediately in .NET Core

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Csala