Jump to content

Recommended Posts

Hi, I'm new here...hoping someone can help. I've been searching for an answer for days:

 

Main question: Do the resources returned by socket_accept automatically take on the blocking behavior of the master socket that spawned them? Or do they default to blocking mode?

 

I'm running a socket server I wrote that uses a non-blocking master socket. Let's call it:

$this->sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_set_nonblock($this->sock);
$a = @socket_bind($this->sock, "localhost", 9934);
socket_listen($this->sock);

 

Now I know the socket won't block on socket_accept. When I want to add a new client, I check if there's something to read:

$r = array($this->sock);
socket_select($r,$w=NULL,$e=NULL,0,15);
if (count($r)==1) {
   $newclient = @socket_accept($this->sock);
}
@socket_read($newclient,1024); //this appears not to block...usually. Why not?

 

My question is, do I have to socket_set_nonblock($newclient) now? Because it seems that 99% of the time, that client won't block on read. However the (very slim) PHP manual documentation on socket_set_nonblock() says:

"Parameters: socket A valid socket resource created with socket_create() or socket_accept()"

 

And although I then go on to socket_select() for read every time I want to use that client resource, once in a blue moon it appears to say it can read, but then block instead and destroys my application. Can anyone clarify the granularity of blocking behavior on socket resources created from socket_accept?

Link to comment
https://forums.phpfreaks.com/topic/261636-socket-blocking-questions/
Share on other sites

If you want it to be non-blocking, then set it to non-blocking mode.  It doesn't really matter how it is setup from the accept call.  From what you describe, it sounds like it probably defaults to blocking mode rather than taking on the same as the server socket.  There's no harm in calling socket_set_nonblock on a socket that's already in non blocking mode, so just do it and be sure.

 

 

Pardon but that's not really an acceptable answer; I need to know whether it can be controlled on a granular level. Can I make a master socket block, one child block, and another nonblock? More importantly, what is the rule that governs their behavior... that's what I want to know. I know what it does in practice. It seems to accept the parent's behavior. What I want is a logical explanation for why it behaves the way it does, and a strict rule which isn't laid out in the PHP manual or anywhere else I could find online. That's why I'm posting here.

 

If you don't know, don't feel obligated to respond.

All sockets and file descriptors have their own blocking/nonblocking status.  You can change it however you wish.  Aside from the fact that the "child" socket was created by an accept call from the "parent" the two do not relate in any way, there is no actual child/parent relationship there.

 

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.