[olug] questions about socket programming

Lou Duchez lou at paprikash.com
Sat Aug 1 20:26:15 CDT 2020


You folks know your socket programming inside out (I bet), so a couple 
questions about a PHP socket server I've written.

First thing the code does is create a socket (let's call it 
"$greeter_socket") for fielding incoming connections.  Then a grand loop 
happens where I iteratively check for incoming connections on 
$greeter_socket, create sockets for bidirectional communications for 
each incoming connection, and also do all the reads / writes to all the 
bidirectional sockets as needed.  Here are my questions:

1) What happens if two people are trying to connect at the same time?  I 
imagine that the socket can handle only one connection at a time, so 
$greeter_socket will handle whoever got there first, and the other 
person will have to try again and again until they get through or their 
retry period expires.

2) The code examples I've seen do not put any sort of sleep(), usleep(), 
or time_nanosleep() in the grand loop. That seems ill-advised to me; 
based on my testing, the grand loop consumes a ton of CPU unless I put a 
brief sleep (maybe one-tenth second) in each iteration.  Am I wrong to 
put the sleep in there?  Am I handling it wrong?


More information about the OLUG mailing list