From: Ryan Sun

>   Stream and networking programming seems like a rock on the way to
> ZCE for most people, so I'm learning some socket examples before I sit
> in the room for exam.
> Here is the script for server


> server&client hangs after output and time out later.
> Can any1 point out whats the reason and the more correct way to get
> socket client input in socket server?

I have not done any socket programs in PHP, but I have in Assembler, C
and Perl. First, I don't think feof() will do what you think it does. I
wouldn't expect it to show up until after the other end has actually
closed the connection.

The other problem has to do with thinking an fread() will always give
you everything you sent in an fwrite(). TCP is a stream protocol, there
are no guarantees about delivering a complete message in one read, or
that two writes won't be read together. It only guarantees that all
octets will eventually be delivered in the same order they were sent, or
you will get an error. The buffering is completely hidden and outside of
your control. If you want writes to be atomic, you want UDP, but then
you lose the guarantee of delivery. If you want to enforce a structure
on the data in that stream, it is your application's responsibility to
reconstruct that data at the receiver.

One other detail that may or may not make a difference. TCP actually
defines two independent pipes, one in each direction. Many Unix
applications create two processes to service a socket, one to send, the
other to receive. Only occasionally does a protocol require alternating
messages similar to a conversation or ping-pong match.

Bob McConnell

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to