Search code examples
rubysocketstcp

Ruby TCP socket never gets replies


I use a TCP connection and send some data like this:

begin
  session = "mysession"
  socket = TCPSocket.new(tcpaddress, port)
  socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
  puts "sending to socket HELO " + session
  socket.write ('HELO ' + session)
  puts socket.read
  socket.close
rescue Exception => myException
   puts "Exception rescued : #{myException}"
end

The socket never gets a reply, however, telnet does:

$ telnet some_ip port
Trying some_ip...
Connected to some_ip
Escape character is '^]'.
HELO mysession
OK

As you can see the remote server replies "OK" as expected. What's wrong?

PS: I have used puts and send methods also.


Solution

  • Perhaps you need to add CRLF to the command:

    socket.write("HELO " + session + "\r\n")