[olug] questions about socket programming

Lou Duchez lou at paprikash.com
Sat Aug 1 22:06:56 CDT 2020


Thanks! One thing the tutorial clued me into is that it's possible to 
set a backlog on "socket_listen", so I can set a buffer on pending 
connections.  That doesn't change how one codes the socket connection, 
but it seems like it will keep from sending a "busy" signal to every 
connection past the first.

That tutorial doesn't have any examples of putting a "sleep" in the 
grand loop, though.  I am caught between believing they know what 
they're doing, but also seeing with my own eyes that a lack of a "sleep" 
means eating up a ton of CPU cycles.  I am not so prideful that I assume 
I know better than the tutorial writers, on the other hand the evidence 
of my senses tells me that they left something out.


On 8/1/2020 10:28 PM, Jeff Hinrichs - DM&T wrote:
> Not sure what you are trying to accomplish,  but this is a decent tutorial
> https://webmobtuts.com/backend-development/introduction-to-php-sockets-programming/
> what happens depends on if you socket code is async or not and how you
> write it will determine
> if that is possible.  The link above shows how to accomplish it in PHP (or
> at least says it does.)
> I have not tested the code or used PHP in quite some time.  So you milage
> may vary or someone with more recent PHP fu
> will pipe up.
>
> Good luck with your endeavors
> -Jeff
> The other thing I was alluding to is web sockets.
>
> On Sat, Aug 1, 2020 at 8:26 PM Lou Duchez <lou at paprikash.com> wrote:
>
>> 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?
>> _______________________________________________
>> OLUG mailing list
>> OLUG at olug.org
>> https://www.olug.org/mailman/listinfo/olug
>>
>


More information about the OLUG mailing list