Jump to content

recognizing closed sockets


benjam

Recommended Posts

I have a couple of scripts where one is an AJAX socket 'client' and the other is a socket server, both in PHP.

 

The nature of the AJAX beast is such that, it must reconnect every time it sends data to or from the socket server.

 

If any data comes in while this reconnect is taking place, that data is lost to the AJAX socket client.

 

I have found a way around this using buffers:  the incoming data gets stored in a buffer, if the client is there, the buffer is sent and cleared.

If the client is not there, the buffer continues to be saved until the client returns, at which time the buffer is sent and the process repeats.

 

The problem I am having is, if the client disconnects, for whatever reason (but to return later), the server does not realize the client has disconnected until after it has sent the next line and loops back to try to read from the disconnected client, which fails, and it then disconnects the client from the local side, and begins to store the buffer contents.

 

But the first line is already deleted from the buffer.

 

I have tried testing FALSE against socket_read($client_sock, 0), socket_write($client_sock, ''), socket_getpeername($client_sock, $null), and all with no luck.

 

The only way the server seems to notice that the client has disconnected is when it tries to read more than a zero length data stream (which I cannot test against, for there may be other clients sending data through those socket_reads, in which case, the valid incoming data will be lost).

 

Does anybody have any suggestions on how I might test for a valid connection before sending my buffer contents, one which does not disrupt any other clients, and works every time?

 

Thanks

 

simple code samples avail upon request

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.