[Sussex] Sockets - PHP, Fedora Core 3

Brendan Whelan b_whelan at mistral.co.uk
Thu Nov 10 11:02:07 UTC 2005


Steve,

Thanks for the response. Yes it is for an TCP connection but there will only
be one client system supplying data so I may only need a straightforward
mechanism. To help matters further, the client will only be sending under
100 messages a day each up to only 100 characters. The sender will await an
ACK from me and so will resend any message where a confirmation is not
received.

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

> _______________________________________________
> Sussex mailing list
> Sussex at mailman.lug.org.uk
> E-mail Address: sussex at mailman.lug.org.uk
> Sussex LUG Website: http://www.sussex.lug.org.uk/
> http://mailman.lug.org.uk/mailman/listinfo/sussex






More information about the Sussex mailing list