How can I force netcat to send my input immediately, not just on newlines?

36

12

How can I force netcat to send my input immediately, not just on newlines? I want to test an HTTP parser manually and check how it behaves when header lines are spread across multiple packets.

thejh

Posted 2012-05-26T15:30:08.287

Reputation: 1 067

2

try socat as listed in this answer

– Jeremy W – 2012-05-26T15:44:56.637

Answers

43

Use the "eof" key, CtrlD. When pressed in the middle of a line, it will give to netcat everything that has been input at that point.

The buffering is actually done by the tty layer and not handled by nc at all. stty -icanon && nc ... would disable the buffering and allow nc to see the data as it is entered into the terminal, at which point it will be sent right away. (Note that the stty and nc commands must be run together, otherwise the shell itself would likely reenable it when displaying its prompt.)

user1686

Posted 2012-05-26T15:30:08.287

Reputation: 283 655

I am using stty -icanon -echo && nc ... otherwise every char I typed in nc get echoed twice – Gelin Luo – 2019-10-09T00:17:10.003

+1, but is there no option for opening netcat up the way that it does not buffer anything? – lpapp – 2014-06-05T13:32:00.207

5@FinalContest: The buffering is done by the tty layer. stty -icanon && nc ... or stty raw && nc ... would disable it. [Note that it must be run together, otherwise the shell itself would reenable it when displaying the prompt.] – user1686 – 2014-06-05T13:52:21.267

@grawity: awesome, the former works fine, but the latter gets stuck... ctrl-c does not work anymore to quit the session, and I cannot get ctrl-z to work on it either. Is that expected? Also, please update your answer so that we can clean the comments up. – lpapp – 2014-06-05T14:07:46.063

2@FinalContest: Yeah, raw changes a whole bunch of tty options, one of them being the handling of special "control" keys at tty level. (It is really a "raw" mode, in that it passes everything to the program.) You can use stty -a -F /dev/pts/XX from another tty to see the current parameters. – user1686 – 2014-06-05T14:23:46.940