Socket Behaviour

Simon Marlow simonmar@microsoft.com
Mon, 4 Jun 2001 11:29:38 +0100


> Can anyone tell me why the following code doesn't work as=20
> expected? Both the server and client hang. If I run
>=20
> server 20000 &
> and=20
> client <hostname> 20000
>=20
> the server logfile produces
>=20
> [dom@lhrtba8fd85 twotest]$ more log.txt
> Thu May 31 14:35:39 BST 2001
> Starting logging
> Thu May 31 14:36:08 BST 2001
> Hello world    48 65 6c 6c 6f 20 77 6f 72 6c 64              =20

It only produces this logfile if you kill the client with ^C before
killing the server.

What's happening is this:  the client writes the string "Hello World" to
the socket.  The server picks it up, but it is waiting for an end of
file before returning the string, so it hangs.  When you kill the
client, the server finally gets EOF and returns Partial "Hello World"
from getBuffer. =20

At this point, the server gets a SIGPIPE because it attempts to write to
a socket whose other end has disappeared.  The default behaviour when
receiving a SIGPIPE is to terminate silently (this behaviour has caught
me out a couple of times - the documentation for Socket mentions it), so
this is what the server does, leaving the logfile that you see above.
If you arrange for the server to ignore SIGPIPE, by adding the line

	installHandler sIGPIPE Ignore Nothing

and importing Posix, then you'll see the broken pipe error from the
server.

Cheers,
	Simon