I am developing an Tcp client in C# and I am using the TcpClient class.
I am not able to connect to the server.
Debugging the application I can see that the call to Connect is successfull but after I do this , I check with netstat the status of the connection on both server and client. The result is that the server the connection is ESTABLISHED, but on the client I cannot see any connection to the server in netstat result.
Reading the TcpClient instance I can see that the property Connected is true so it should be fine but when I try to read from the NetworkStream it hangs.
When I say it hangs I mean that the server is sending data but the readline doesn’t get any data at all.
Do you know what could be the issue and some workaround?
Thanks
First, a recommendation: In my experience, TcpClient is best used asynchronously.
In all my usage of TcpClient though, I’ve never been able to Read a response without first doing a Write with a request. You’ll block forever attempting to synchronously await a response to a request you haven’t sent.
Expanding on that, sending a request will be done like this:
Which will send the request that’s in tcpMessage, which will be a bytestream produced from a string like this:
Which has your request message like this:
That sends your request, which causes the server to generate a response which you can then collect like this:
And you should finally be able to receive something back!
Even if it’s only a “I didn’t like your request!” response. 8 D