Search code examples
c#httpclienttcpclienttcplistenernetworkstream

HTTP Client to TCP Server (C#)


I am not receiving the Content posted by the HTTPClient but I can read the other information such as Content-Length and other headers. Let me explain the code here:

Here is the Server Code :

 TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"),1234);
tcpListener.Start()//Start the listener and wait for client to connect.
while(true)
{
 TcpClient newclient = tcpListener.AcceptTcpClient(); // calls the readdata everytime a post is put in the specified URL - done by client below.
 ReadData(newClient)
}

public void ReadData(TcpClient newclient)
{
            byte[] buffer = new byte[50];
            Stream ns = newclient.GetStream();     
            ns.Read(buffer, 0, buffer.Length);
            Console.WriteLine( Encoding.UTF8.GetString(buffer));
 }
Sample Server Output :


 Received JSON DATA POST /MovieData HTTP/1.1
Host: 127.0.0.1:3291
Content-Length: 48
Expect: 100-continue
Connection: Keep-Alive - But the Content is missing. I am not sure why. I tried to extend the buffer size but still Content Length and other info is posted but content is missing. 

Here is the Client Code : Client keeps sending message "Sending Request in a loop

                    HttpClient ModifyClient = new HttpClient();
                    ModifyClient.BaseAddress = new Uri("http://127.0.0.1:1234/MovieData");
                    while(true)
                   {                        
    
                    ModifyClient.PostAsync(ModifyClient.BaseAddress
                                   , new StringContent("SendingRequest",Encoding.UTF8));
                   }

I am able to receive the post message as shown in the server for every post but what is missing is that is the string content that is actually sent "SendingRequest" text. The other header properties are there. Is it some configuration that I am missing while using the HttpClient ?


Solution

  • There is serveral issues in your server: Missing Semicolon:

    tcpListener.Start()//Start the listener and wait for client to connect.
    

    newclient vs newClient and missing semicolon after ReadData()

    TcpClient newclient = tcpListener.AcceptTcpClient(); // calls the readdata everytime a post is put in the specified URL - done by client below.
                ReadData(newClient)
    

    The second issue is, that you are using HTTPClient to connect to a TCP-Server, you should use TCPClient and send a STREAM (this is what you expect!)

             TcpClient client = new TcpClient("/127.0.0.1", 1234);
             string message = "myMessage";
             // Translate message into ASCII and store it as a Byte array
             Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
    
             // Get a client stream for reading and writing.
             NetworkStream stream = client.GetStream();
    
             // Send the message to the connected TcpServer.
             stream.Write(data, 0, data.Length);