Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby TCPSocket write doesn't work, but puts does?

Tags:

ruby

tcp

sockets

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.
like image 697
nathan Avatar asked Jul 18 '09 15:07

nathan


3 Answers

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"?

like image 110
Duck Avatar answered Oct 04 '22 14:10

Duck


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.

like image 36
Greg Avatar answered Oct 05 '22 14:10

Greg


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.

like image 25
Bkkbrad Avatar answered Oct 01 '22 14:10

Bkkbrad