[Gllug] OT: reading from a socket in php

Walter Stanish walter.stanish at saffrondigital.com
Wed Oct 6 09:24:58 UTC 2010


It's a rare day when socket programming in PHP is particularly useful.  However,
if you really want to do it, some advice might be to copy some sample code from
http://php.net/socket_read then modify it until it works.  Start with
just the connect
and read (ala nc ...), and expand to include the initial trigger thereafter.
Later, if you are motivated, go back and figure out what you did differently.

Sounds like a socket or read option is causing it to wait for more data before
returning, since you can see the data coming in over the wire and have not
asked for one byte at a time.  The documentation for socket_read (linked above)
states that it's based on the system recv() documentation, whose manpage
states:

       If  no messages are available at the socket, the receive calls wait for
       a message to arrive, unless the socket is non-blocking (see  fcntl(2)),
       in  which case the value -1 is returned and the external variable errno
       is set to EAGAIN or EWOULDBLOCK.  The receive calls normally return any
       data  available,  up  to  the requested amount, rather than waiting for
       receipt of the full amount requested.

       The select(2) or poll(2) call may be used to determine when  more  data
       arrives.

I am pretty sure you can study the above and find the solution.

Not sure that "do{ ... } while($recv=='')" is a good choice of control
structure,
or is going to work right, either.

- Walter
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list