How to send one byte symbol to server socket with netcat?
Asked Answered
I

1

6

There's already a working server service via socket, and I would like to test it via netcat. I'm using Mac OS X Lion. Currently, I'm able to connect to server via port, to send packet, but the packet contains wrong value. Here are the details:

I need to send 'm' symbol to the server and the server will return 00000000, a zero byte as a response. Server guy told me, server receives 'A0' when I'm sending 'm', and server receives '313039A' when I'm sending '109'. How to define sending format or something, I just need to send 'm' (01101101)?

Infirm answered 7/2, 2012 at 14:29 Comment(1)
Don't know about A0, but in ASCII 109 is 0x31 0x30 0x39.Skinflint
B
19

You can send just "m" with

echo -n 'm' | nc <server> <port>

You can easily check what you're sending on your local machine:

# in one Terminal start the listener:
$ nc -l 1234 | hexdump -C
00000000  6d                                                |m|
00000001

# in other Terminal send the packet:
$ echo -n 'm' | nc 127.0.0.1 1234

nc will happily send/receive NUL bytes - there is no problem with that:

# sending side
$ echo -n X | tr X '\000' | nc 127.0.0.1 1234

# receiving side
$ nc -l 1234 | hexdump -C
00000000  00                                                |.|
00000001
Balcer answered 7/2, 2012 at 14:35 Comment(5)
Thanks a lot. However, the server does not respond when I'm sending just 'm' via nc. It does when I'm sending manually formed packet from my program (objective-c). Besides, what 00000000 and 00000001 means in the example? I assume, '6d' is 'm' but what other values mean? IMHO it sends 3 bytes: null byte, value of 'm' and size of packet.Infirm
But I need to send only 1 byte: just value of 'm' (01101101). Thats how the server is implemented. This is a handshake package.Infirm
No the 000000 is the address - 0-based so it says there is only one single byte 0x6d which is exactly 'm'Balcer
OK. Actually, the server sends zero byte as a response after getting handshake 'm'. Maybe everything is OK and I do send 'm' to the server, but maybe netcat does not display a zero byte. After this operation the connection is established and I could send other packets and could see results from them, but not sure how to enter exact bytes when making second request. After echo .. nc, the cursor is in blank new line and IMHO all entered symbols are interpreted as ASCII symbols, not bytes.Infirm
netcat will happily receive a NUL character and pass it on - I've updated the answer with the output to demonstrate thatBalcer

© 2022 - 2024 — McMap. All rights reserved.