Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)

share|improve this question

2 Answers 2

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
share|improve this answer
    
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 Jun 10 '13 at 7:34
    
@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 Jun 10 '13 at 10:27

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.