Jump to content

Recommended Posts

Guys,

 

I am trying to write some code that looks for any new attempts to connect to a socket whilst servicing other sockets that may already be open (so many clients can connect and be at various stages of being serviced - like most multithreaded programs do i guess).

 

Below is some code from www.php.net (http://uk.php.net/manual/en/function.socket-accept.php#82618) which is just the beginning of my code (i intend to store newc in an array and if valid then service during the while loop, HOWEVER, the demo program (although runs and preforms -i.e. multiple connections at any 1 time) it throws lots of (and I mean lots!!!!!) into the apache log file (which isnt going to work): -

[Mon Nov 24 20:22:48 2008] [error] [client 127.0.0.1] PHP Warning:  socket_accept() [<a href='function.socket-accept'>function.socket-accept</a>]: unable to accept incoming connection [0]: A non-blocking socket operation could not be completed immediately.\r\n in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\socketin.php on line 11

[Mon Nov 24 20:22:48 2008] [error] [client 127.0.0.1] PHP Warning:  socket_accept() [<a href='function.socket-accept'>function.socket-accept</a>]: unable to accept incoming connection [0]: A non-blocking socket operation could not be completed immediately.\r\n in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\socketin.php on line 11

 

 

<?php

 

$clients = array();

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

socket_bind($socket,'0.0.0.0',5555);

socket_listen($socket);

socket_set_nonblock($socket);

 

while(true)

{

    if(($newc = socket_accept($socket)) !== false)

    {

        echo "Client $newc has connected\n";

        $clients[] = $newc;

    }

}

 

?>

Any ideas? It makes sense to be non blocking i guess as need to continue to loop to service other connections and cant "wait" for new connections!!

Link to comment
https://forums.phpfreaks.com/topic/134100-multiple-sockets-help-please/
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.