EOF and netcat

EOF and netcat

am 27.08.2007 15:22:32 von Frederic Mayot

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

Re: EOF and netcat

am 27.08.2007 20:02:42 von Bill Marcum

On Mon, 27 Aug 2007 06:22:32 -0700, Frederic Mayot
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.

Re: EOF and netcat

am 27.08.2007 21:19:06 von Frederic Mayot

On Aug 27, 2:02 pm, Bill Marcum wrote:
> On Mon, 27 Aug 2007 06:22:32 -0700, Frederic Mayot
>
> 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.

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

Re: EOF and netcat

am 28.08.2007 05:41:33 von Barry Margolin

In article <1188220952.431286.71340@22g2000hsm.googlegroups.com>,
Frederic Mayot 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 ***

Re: EOF and netcat

am 28.08.2007 23:23:59 von William Park

Frederic Mayot 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 , Toronto, Canada
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/