Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does listen() run continuously or do I need to loop it to keep receiving connections on a socket?

I'm running a client and server on the same machine using a loopback address for learning purposes yet my "server" code seems to fly back listen() and then hangs on my connect(). Does listen() need to be in an endless loop to keep receiving connections?

How would I determine a connection is made/in the queue if listen() returns 0 even when I haven't made a connection yet?

I have an accept() call following but the code hangs on that spot. I have debug statements right before and after and it never gets past the accept().

On the other end my client code seems to connect() just fine (doesn't throw an error) and appears to write and complete even though the server code never gets the connection.

like image 502
Milan Novaković Avatar asked Jan 21 '26 23:01

Milan Novaković


1 Answers

The listen function defines the backlog. Only need to call this once.

Then use accept to receive an incoming connection. Best to deal with this promptly and go around again for another accept.

like image 105
Ed Heal Avatar answered Jan 24 '26 14:01

Ed Heal