require 'socket'
server = TCPServer.open(2000)
loop {
Thread.start(server.accept) do |client|
File.open("tmp","w") { |file| file.write(client.gets)}
File.open("tmp").each do |f|
puts f.unpack('H*')
end
client.puts(Time.now.ctime) # Send the time to the client
client.puts "Closing the connection. Bye!"
client.close # Disconnect from the client
end
}
接收的数据如下: xx ^ Q ^ A ^ Hb0 @< 90> 26 2 ^ B ^ @< 83> ev
我想这样: 787811010862304020903236202032020001c26c0d0a
抱歉我的英语不好!答案 0 :(得分:2)
如果有多个客户端发送数据,则使用带名称的临时文件会导致问题;临时文件将被覆盖。
您无需使用临时文件。
require 'socket'
server = TCPServer.open(2000)
loop {
Thread.start(server.accept) do |client|
puts client.gets.unpack('H*')
client.puts(Time.now.ctime) # Send the time to the client
client.puts "Closing the connection. Bye!"
client.close
end
}