[Sussex] Sockets - PHP, Fedora Core 3

Steve Dobson steve at dobson.org
Thu Nov 10 10:44:12 UTC 2005


Brendan

On Wed, Nov 09, 2005 at 08:37:09PM -0000, Brendan Whelan wrote:
> I am attempting to "perfect" a socket driver which will received lines
> of text from another system.  The heart of the code, without the initial
> sockets opening instructions and debug messages is shown below.
> 
> Messages are received from the other system but the timeout isn't working
> properly.  The results I get when no data is being transmitted from the
> other end is:
> 
> 1. The read is completed several times but the buffer is empty and the
>    time from before to after the read is zero i.e. it completes the read
>    instantly.
> 
> 2. I then get a couple of reads which wait for 10 seconds each returning
>    an empty string. (This is almost what I want)
> 
> 3. Return to step 1.
> 
> I would expect the read to timeout after 30 seconds, return an empty
> buffer and then wait for another 30 seconds.
> 
> Any suggestions much appreciated.
> 
> -------------------------------------------------------------------------
> while ($continue == "Y")
> {
>     if ($conn != "")
>     {
>       $result = socket_close($conn);
>     }
> 
>     $conn = socket_accept($sock);

This appears very, very wrong to me.  I assume that this is a TCP socket
and that you want to accept the connection from a new client (maybe the
first, maybe no).  If a second client is connecting you loose the accept
connection to the first, if there isn't a second connection then I'm
I would expect this to block.
 
>     support_f_socket_timeout($conn);
> 
>     $received = socket_read($conn, 4096);
> }

I would suggest that you don't use the SO_RCVTIMEO, these are more to
do with error reporting than for not getting data down a stream, and
not getting data down a stream is NOT and error condition.

Take a look at socket_select() which is there to wait for input from
a number of sockets.  That way you can monitor the socket that your
clients connect to and the multiple client connections all that the
same time.

Steve
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.lug.org.uk/pipermail/sussex/attachments/20051110/732961cd/attachment.pgp 


More information about the Sussex mailing list