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

后端 未结 7 679
走了就别回头了
走了就别回头了 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 12:11

    We need to follow the best practice on writing client -server programing. The best guide I can recommend you at this time is The C10K Problem . There are specific stuff we need to follow in this case. We can go for using select or poll or epoll. Each have there own advantages and disadvantages.

    If you are running you code using latest kernel version, then I would recommend to go for epoll. Click to see sample program to understand epoll.

    If you are using select, poll, epoll then you will be blocked until you get an event / trigger so that your server will not run in to infinite loop by consuming your system time.

    On my personal experience, I feel epoll is the best way to go further as I observed the threshold of my server machine on having 80k ACTIVE connection was very less on comparing it will select and poll. The load average of my server machine was just 3.2 on having 80k active connection :)

    On testing with poll, I find my server load average went up to 7.8 on reaching 30k active client connection :(.

    0 讨论(0)
提交回复
热议问题