2012-08-17 117 views
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 
+0

@ buff1是什麼? – PriteshJ 2012-08-17 11:04:17

+0

它只是一個文章 – 2012-08-17 11:06:57

+0

plz添加數組@buff和你用來測試的示例url – PriteshJ 2012-08-17 11:11:14

回答

0

什麼你正在尋找從Ruby的文檔是Array.packString.unpack

示例代碼:

對於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"] 
相關問題