[Sussex] Setting up a socket - PHP/Fedora
Ronan Chilvers
ronan at thelittledot.com
Tue Sep 13 09:48:10 UTC 2005
Hi Brendan
On Mon, 12 Sep 2005 20:14:03 +0100
"Brendan Whelan" <b_whelan at mistral.co.uk> wrote:
> Hello,
>
> I am attempting to create a simple sockets program in PHP. I have
> created a server and a client routine - see below. The server starts
> and then waits for a message when the client sends a message the
> server confirms receipt and then tries to send a reply. At this point
> I get a failure. If I run through a browser I get a "socket[32]
> broken pipe" error but when run in a terminal window I get "File size
> limit exceeded". The output from both programs is shown below.
The server needs to run continuously so you want to run it from a
command line like so
php -q server.php
This will run the process and keep it running. You can't run the
server from a browser. You could run the client though but whether
its a good idea or not depends on exactly what the application is.
Secondly, in your client you're still binding the socket to an IP. You
don't need to do this. The client doesn't need to bind since its just
making a connection to a 'remote' listening port. You could for
example use fsockopen() to connect and send to your server.
Secondly there are some syntax errors in your server code. Most
notably on socket_accept where you've used the wrong variable to store
the return value. socket_accept returns a connected socket as a file
handle, which you then use in your subsequent communication with that
specific connection. You don't use the original listener. In other
words when a connection comes into the listening socket, it sets up
another socket hands, the incoming connection to it, and then goes back
to the business of listening, while the new socket handles the new
connection. This is where your broken pipe comes in - you weren't
writing to the correct socket.
I've butchered your scripts a bit to show you what I mean and attached
them herewith (the client doesn't quite do what you want but the
communication happens so you should be able to cobble it together from
there). Hope they work - very quick bash out!!!
Incidentally you might want to look at using streams to do this stuff
instead of the socket extension. They are much easier and more
reliable. Have a look at the stream_socket_* functions.
Any problems do drop me a line.
Cheers
Ronan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: client2.php
Type: application/x-php
Size: 419 bytes
Desc: not available
Url : http://mailman.lug.org.uk/pipermail/sussex/attachments/20050913/c8f1e0a4/attachment.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: server2.php
Type: application/x-php
Size: 1246 bytes
Desc: not available
Url : http://mailman.lug.org.uk/pipermail/sussex/attachments/20050913/c8f1e0a4/attachment-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mailman.lug.org.uk/pipermail/sussex/attachments/20050913/c8f1e0a4/attachment.pgp
More information about the Sussex
mailing list