我运行的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”)
谢谢!
答案 0 :(得分:10)
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