Most efficient way to handle a client connection (socket programming)

后端 未结 7 683
走了就别回头了
走了就别回头了 2021-01-18 11:26

For every single tutorials and examples I have seen on the internet for Linux/Unix socket tutorials, the server side code always involves an infinite loop that checks for cl

7条回答
  •  伪装坚强ぢ
    2021-01-18 11:58

    The infinite loop is there to maintain the server's running state, so when a client connection is accepted, the server won't quit immediately afterwards, instead it'll go back to listening for another client connection.

    The listen() call is a blocking one - that is to say, it waits until it receives data. It does this is an extremely efficient way, using zero system resources (until a connection is made, of course) by making use of the operating systems network drivers that trigger an event (or hardware interrupt) that wakes the listening thread up.

提交回复
热议问题