Ruby TCP套接字永远不会得到回复

时间:2011-05-13 17:23:30

标签: ruby sockets tcp

我使用TCP连接并发送一些这样的数据:

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

套接字永远不会收到回复,但telnet会这样做:

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

正如您所见,远程服务器按预期回复“OK”。怎么了?

先谢谢!

PS:我也使用过put和send方法

1 个答案:

答案 0 :(得分:2)

也许你需要在命令中添加CRLF:

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