gpt4 book ai didi

ruby - 在 ruby​​ 中发送和接收 TCP 数据

转载 作者:可可西里 更新时间:2023-11-01 02:31:55 25 4
gpt4 key购买 nike

我有一个 TCP 服务器正在运行,它接受命令“GETHELLO”并返回“HELLO”。我在 linux shell 中使用 Telnet 测试它:

:~$ telnet 192.168.1.10 3000
Trying 192.168.1.10...
Connected to 192.168.1.10.
Escape character is '^]'.
GETHELLO
HELLO

如何使用 TCPSocket 在 ruby​​ 中执行此操作? (发送“GETHELLO”,读取服务器返回的数据“HELLO”)

谢谢!

最佳答案

require 'socket'
sock = TCPSocket.new('192.168.1.10', 3000)
sock.write 'GETHELLO'
puts sock.read(5) # Since the response message has 5 bytes.
sock.close

关于ruby - 在 ruby​​ 中发送和接收 TCP 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9909554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com