How to send a binary message in TCP and store the response

0

I have to develop a client for a proprietary protocol and it would be very useful to be able to understand the behaviour of the existing server by sending it custom messages and look at the answer. "Telnet" would be perfect for that purpose except that the protocol is binary.

So currently, I have written the message i want (using a hex editor) in a file mymsg and I'm trying to send it using netcat this way:

cat msg | netcat 127.0.0.1 1234

My problem with that is that netcat just stops after it reaches EOF so I never get to see the answer of the server. Any suggestions?

(of course, one can run a mock-up of the proprietary server using nc -l -p 1234)

user229790

Posted 2013-06-07T17:02:04.833

Reputation: 1

Answers

1

I’m a little puzzled, because I thought that netcat had an explicit feature to handle just this case.  I thought that it waited until it had gotten EOFs from both standard input and the socket.  Maybe it’s just a timeout thing; check your netcat documentation to see whether there’s an option to keep on reading from the socket for a certain amount of time after getting EOF on stdin.

Or you can use the kludge answer:

(cat msg; sleep 42) | netcat 127.0.0.1 1234

Scott

Posted 2013-06-07T17:02:04.833

Reputation: 17 653

I guess the option you mention is -w N . It doesn't work out of the box for me :( . Also I might be missing something but the kludge solution doesn't take input while it is in the sleep part so that's an issue! – user229790 – 2013-06-10T07:34:00.813

@user229790: I think -w does something different, it explicitly says "If a connection and stdin are idle for more than...", EOF is not idle. -i on the other hand sounds like it's worth a try, "Specifies a delay time interval between lines of text sent and received.". – Bobby – 2013-06-10T10:27:46.827

0

I came up with this python script to do the job.

#!/usr/bin/env python

import socket
import sys

if (len(sys.argv) != 3):
    print "usage: " + sys.argv[0] + " host port <message >answer"
    print ""
    print "if you want to create a fake server side: nc -l -p port"
else:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((sys.argv[1], int(sys.argv[2])))
    msg = sys.stdin.read()
    s.send(msg)
    answer = s.recv(1024)
    print answer

user229790

Posted 2013-06-07T17:02:04.833

Reputation: 1