上代码 server
require 'socket'
server = TCPServer.open('127.0.0.1',2000)
client = server.accept
loop {
str=client.read
puts str
client.write(str)
}
client.close
client
require 'socket'
host = '127.0.0.1'
port = 2000
msg="\0x48\0x0a\0x0d"
s = TCPSocket.open(host, port)
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
loop do
send = s.write(msg)
s.flush
puts msg
resp = s.read
puts resp
sleep 1
end
google 了一下,别人也有相同的问题, 按照这上面说的 http://stackoverflow.com/questions/1147852/ruby-tcpsocket-write-doesnt-work-but-puts-does 加上 s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) 和 flush 也不管用