我正在创建一个套接字服务器。此服务器正在处理十六进制值。
我能够建立与服务器的连接。但是无法读取客户端的十六进制值,也无法将十六进制值放到客户端。
require "socket"
def listenserver (server)
@buff1 =[160, 124]
client = server.accept
puts "#{client.inspect} is accepted"
client.print "0x%02x" % @buff1[0]
client.print"0x%02x" % @buff1[1]
puts "#{@buff1.inspect}"
puts "0x%02x" % @buff1[0]
client.close
puts "#{client} is gone"
end
server = TCPServer.new('x.x.x.x', 12345)
loop do
puts "Started TCPServer"
listenserver(server)
end
答案 0 :(得分:0)
您要找的是Array.pack和String.unpack
Ruby Docs中的示例代码:
Array.pack :
a = [ "a", "b", "c" ]
a.pack("A3A3A3")
=> "a b c "
和 Sting.unpack :
"\xfe\xff\xfe\xff".unpack('sS')
=> [-2, 65534]
"whole".unpack('xax2aX2aX1aX2a')
=> ["h", "e", "l", "l", "o"]