tags:

views:

1036

answers:

3

I'm working on a Ruby TCP client/server app using GServer and TCPSocket. I've run into a problem that I don't understand. My TCPSocket client successfully connects to my GServer, but I can only send data using puts. Calls to TCPSocket.send or TCPSocket.write do nothing. Is there some magic that I'm missing?

tcp_client = TCPSocket.new( ipaddr, port )
tcp_client.puts( 'Z' ) # -> GServer receives "Z\n"

But if I use write or send...

tcp_client = TCPSocket.new( ipaddr, port )
tcp_client.write( 'Z' ) # -> nothing is received
tcp_client.send( 'Z' ) # -> nothing is received

Thanks for the help

Additional information:

  1. The behavior is the same on Linux & Windows.
  2. Flushing the socket after write doesn't change the behavior.
+1  A: 

Try explicitly flushing:

tcp_client = TCPSocket.new( ipaddr, port )
tcp_client.write( 'Z' ) 
tcp_client.send( 'Z' ) 
tcp_client.flush

This way, the output is buffered at most only until the point at which you decide it should be sent out.

Bkkbrad
Thanks brad, but see my comment above. There was no difference when calling flush.
nathan
+2  A: 

Are you sure the problem isn't on the server side? Are you using some method to read that expects a string or something ending in "\n"?

Duck
I thought I had used recvfrom for the server side, but it turned out I was using gets, which is looking for the newline character.
nathan
A: 

With buffering taken care of in previous posts to address the question of whether the data is being sent consider capturing the data on the line using something like wireshark. If the data you are sending is seen on the line then the server isn't receiving it.

Otherwise, if the data isn't going onto the line, TCP may hold onto data to avoid sending a single segment with only a few bytes in it (see Nagle's Algorithm). Depending on your OS or TCP vendor you may have different behaviour, but most TCP stacks support the TCP_NODELAY option which may help get the data out in a more timely manner.

tcp_client.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

This can help debugging, but typically shouldn't be left in production code if throughput is higher priority than responsiveness.

Greg
netcat is a very useful tool as well for testing purposes."nc -v -n -l -p 12345" will make a listening server on port 12345 that will print the data immediately as it receives it.
Andrew Y