0

我正在创建一个套接字服务器。此服务器正在处理十六进制值。

我能够建立与服务器的连接。但无法读取客户端十六进制值,也无法将十六进制值发送到客户端。

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
4

1 回答 1

0

您正在寻找的是Array.packString.unpack

来自 Ruby 文档的示例代码:

对于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"]
于 2012-08-18T08:28:40.510 回答