Search code examples
ruby-on-railsrubysocketstcptcpclient

Rails TCP Socket


I'm trying to send byte array to network control, I send this data from hex terminal from windows machine and it worked correctly and I got the right response then using rails console on Ubuntu I tried

host = "192.168.1.100"
port = 10001
Socket.tcp(host , ip) do |sock| sock.puts([0x01,0x00,0x00,0x00,0x00,0x21,0x04,0x61,0x64,0x6D,0x69,0x6E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x61,0x64,0x6D,0x69,0x6E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC8]) end

I got nil as response, and when I tried

Socket.tcp(host , ip) do |sock| sock.write([0x01,0x00,0x00,0x00,0x00,0x21,0x04,0x61,0x64,0x6D,0x69,0x6E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x61,0x64,0x6D,0x69,0x6E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC8]) end

I got 141 as response but the right response is (00 00 01 00 00 01 01 FD 00 00 01 00 01 01 02 FB)

I do not know what's the problem, can any one help me? Thanks in advance.


Solution

  • the problem was in the bytes array format, it should be send as string in this format "\x01\x00\x00\x00\x00\x21\x04\x61\x64\x6D\x69\x6E\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x61\x64\x6D\x69\x6E\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" then get the response using response = socket.recv( 1000 )