【问题标题】:Ruby TCP socket never gets repliesRuby TCP 套接字永远不会得到回复
【发布时间】:2011-05-13 17:23:30
【问题描述】:

我使用 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:我也使用了 puts 和 send 方法。

【问题讨论】:

标签: ruby sockets tcp


【解决方案1】:

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

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

【讨论】:

  • 是的,已经解决了,谢谢:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 2021-08-04
  • 2016-10-17
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多