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

发布于 2024-11-07 06:42:48 字数 622 浏览 0 评论 0原文

我使用 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 方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2024-11-14 06:42:48

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

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

Perhaps you need to add CRLF to the command:

socket.write("HELO " + session + "\r\n")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文