TcpClient doesn't connect
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 b开发者_如何学Ce 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:
TcpClient.GetStream().BeginWrite( tcpMessage, ... );
Which will send the request that's in tcpMessage, which will be a bytestream produced from a string like this:
byte[] tcpMessage = httpEncoding.GetBytes( httpMessage );
Which has your request message like this:
httpMessage = "GET / HTTP/1.1\r\n" + ...;
That sends your request, which causes the server to generate a response which you can then collect like this:
TcpClient.GetStream().BeginRead( ... );
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
What you saw is absolutely normal.
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 you try to read, the thread will be blocked till the server sends you some data. Otherwise, based on the read method you used, it can be blocked forever.
It depends on the server, but most TCP servers will wait for the client to send a request before it sends a response. It sounds like your thread is waiting for data. Did you set a read timeout?
Make sure you send a request so that the server knows to send you a response.
Normally, you would put your TcpClient into a separate thread and use a read time out to avoid hanging the whole program awaiting a response.
Make sure that you don't use non-blocking (asynchronous) sockets when communicating with a blocking (synchronous) server, you'll only run into problems. Blocking sockets are normal for the web, as most web services use the request/response paradigm.
on client side, what port were you looking for when you did netstat?
Because when client make connections, it would use ephemeral port. Which means it would use any free port # above maximum of well known port number. So, your client maybe using different port # than you are expecting.
And for the networkstream issue, I would have to see the code to determine what went wrong.
精彩评论