[olug] questions about socket programming

Lou Duchez lou at paprikash.com
Sat Aug 1 22:45:51 CDT 2020


I believe you're right about socket_accept.  What I'm doing is I'm first 
checking whether there are any incoming connections via socket_select, 
which has a timeout interval that I set to 10 microseconds -- that's 
enough time to tell me if there are any waiting connections, without 
idling for an extended period.  If there is a waiting connection,  
that's when I invoke socket_accept, so the blocking behavior of 
socket_accept isn't a problem.

The grand loop ("while (true) { ... }") that contains all the connection 
handling, has three parts: accepting new connections, reading data from 
existing connections, and sending data as appropriate to existing 
connections.  The "sleep" part that I feel is a good idea is at the end 
of the grand loop.



On 8/1/2020 11:35 PM, Jeff Hinrichs - DM&T wrote:
> My understanding is that the loop doesn't execute and waits at the
> socket_accept statement
> which has a comment that is is a blocking call.  Which would mean that no
> code beyond that point
> is executed unless there is an incoming socket request.
>
> If you put an echo immediately after that statement, nothing should print
> until an incoming socket occurs.
> You can prove that the PHP loop is not busy by doing so.
>
> -Jeff
>
>
> On Sat, Aug 1, 2020 at 10:07 PM Lou Duchez <lou at paprikash.com> wrote:
>
>> 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
>>>>
>> _______________________________________________
>> OLUG mailing list
>> OLUG at olug.org
>> https://www.olug.org/mailman/listinfo/olug
>>
>


More information about the OLUG mailing list