views:

77

answers:

4

Hi All

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

+1  A: 

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.

Marcus Adams
No and I forgot to mention that When I say readLine hangs I mean that the server is sending data but the readline doesn't get any data at all.
Are you sure that the server is sending data? Most of the web works with the request/response paradigm. You must send a request before the server will respond.
Marcus Adams
+1  A: 

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.

Lex Li
I was not clear. When I say it hangs I mean that the server is sending data but the readline doesn't get any data at all.
A: 

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.

Ankiov Spetsnaz
+1  A: 

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

Task
Sound that your suggestions looks good. Basically my client was only listening to server. When I say ReadLine hangs I mean what do you say: the server is sending data but I wait forever.One more question: when you say "Write with a request" what do you exactly mean in terms of code for Request? Do you mean just NetworkStream.Write()..like somekind of heartbeat?
There you go, answer expanded. It's not a heartbeat signal, it's your TCP request message. Which, in my example, is an HTTP GET. Hope that clears it up for you.
Task