Unix Shell - EOF and netcat

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > August 2007 > EOF and netcat





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author EOF and netcat
Frederic Mayot

2007-08-27, 1:25 pm

Hi,
I'm running a program in background which reads and writes on standard
input/output.
I'm using netcat to send commands and to read the result.

To start my prog:
netcat -l -u localhost -p 5555 | my_program &

To send commands:
echo "my_command" | netcat -u localhost 5555

The problem is since my_program does not close its file descriptor for
output, the "client" netcat never returns. I tried to write special
characters like 0x1a, 0x03, 0x04 to fake an EOF with no success.

Any idea?

Thanks,
Fred

Bill Marcum

2007-08-27, 1:25 pm

On Mon, 27 Aug 2007 06:22:32 -0700, Frederic Mayot
<fred@mayot.net> wrote:
>
>
> Hi,
> I'm running a program in background which reads and writes on standard
> input/output.
> I'm using netcat to send commands and to read the result.
>
> To start my prog:
> netcat -l -u localhost -p 5555 | my_program &
>
> To send commands:
> echo "my_command" | netcat -u localhost 5555
>
> The problem is since my_program does not close its file descriptor for
> output, the "client" netcat never returns. I tried to write special
> characters like 0x1a, 0x03, 0x04 to fake an EOF with no success.
>
> Any idea?
>
> Thanks,
> Fred
>

Change my_program so it does close its output, or make the client close
its output:
echo "my_command" | netcat -u localhost 5555 | head -n 1
or
echo "my_command" | netcat -u localhost 5555 &
sleep 5
kill $!

--
Churchill's Commentary on Man:
Man will occasionally stumble over the truth,
but most of the time he will pick himself up and continue on.
Frederic Mayot

2007-08-27, 7:22 pm

On Aug 27, 2:02 pm, Bill Marcum <marcumb...@bellsouth.net> wrote:
> On Mon, 27 Aug 2007 06:22:32 -0700, Frederic Mayot
>
> <f...@mayot.net> wrote:
>
>
>
>
>
>
>
> Change my_program so it does close its output, or make the client close
> its output:
> echo "my_command" | netcat -u localhost 5555 | head -n 1
> or
> echo "my_command" | netcat -u localhost 5555 &
> sleep 5
> kill $!
>
> --
> Churchill's Commentary on Man:
> Man will occasionally stumble over the truth,
> but most of the time he will pick himself up and continue on.


Thanks for your help but your first solution does not work. I tried
with and without -u option.

Barry Margolin

2007-08-28, 1:20 am

In article <1188220952.431286.71340@22g2000hsm.googlegroups.com>,
Frederic Mayot <fred@mayot.net> wrote:

> Hi,
> I'm running a program in background which reads and writes on standard
> input/output.
> I'm using netcat to send commands and to read the result.
>
> To start my prog:
> netcat -l -u localhost -p 5555 | my_program &
>
> To send commands:
> echo "my_command" | netcat -u localhost 5555
>
> The problem is since my_program does not close its file descriptor for
> output, the "client" netcat never returns. I tried to write special
> characters like 0x1a, 0x03, 0x04 to fake an EOF with no success.


There's no such thing as an EOF character on Unix. EOF occurs when a
stream or file *ends*. In the case of a network socket, you have to
close the descriptor; of, if it's a stream socket, you can call
shutdown(fd, SHUT_WR).

The only exception to this is in the terminal driver, which lets you
fake EOF on input (since the only real "end" to a terminal stream would
be a hangup of the phone line, and this would be really inconvenient).
But this mechanism doesn't map to any other type of device.

Since you're using UDP, the shutdown() mechanism won't work. netcat
waits until the application closes the pipe before it writes anything.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
William Park

2007-08-28, 7:21 pm

Frederic Mayot <fred@mayot.net> wrote:
> Hi,
> I'm running a program in background which reads and writes on standard
> input/output.
> I'm using netcat to send commands and to read the result.
>
> To start my prog:
> netcat -l -u localhost -p 5555 | my_program &
>
> To send commands:
> echo "my_command" | netcat -u localhost 5555
>
> The problem is since my_program does not close its file descriptor for
> output, the "client" netcat never returns. I tried to write special
> characters like 0x1a, 0x03, 0x04 to fake an EOF with no success.
>
> Any idea?
>
> Thanks,
> Fred


Solution is to shutdown the client as soon as "EOF" is reached on its
input. 'netcat' unfortunately does not have -q option that 'nc' has.
Try -w option. As you may have guessed, there are more than one
"netcat". :-)

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com