www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當前位置:首頁 > 公眾號精選 > Linux閱碼場
[導讀]當然可以,只要你使用?SO_REUSEPORT?這個參數(shù)。還是先來看下man文檔中是怎么說的:SO_REUSEPORT(sinceLinux3.9)PermitsmultipleAF_INETorAF_INET6socketstobeboundtoanidenticalsocke...

當然可以,只要你使用?SO_REUSEPORT?這個參數(shù)。


還是先來看下man文檔中是怎么說的:


SO_REUSEPORT (since Linux 3.9) Permits multiple AF_INET or AF_INET6 sockets to be bound to an identical socket address. This option must be set on each socket (including the first socket) prior to calling bind(2) on the socket. To prevent port hijacking, all of the pro‐ cesses binding to the same address must have the same effec‐ tive UID. This option can be employed with both TCP and UDP sockets.
For TCP sockets, this option allows accept(2) load distribu‐ tion in a multi-threaded server to be improved by using a dis‐ tinct listener socket for each thread. This provides improved load distribution as compared to traditional techniques such using a single accept(2)ing thread that distributes connec tions, or having multiple threads that compete to accept(2) from the same socket.
For UDP sockets, the use of this option can provide better distribution of incoming datagrams to multiple processes (or threads) as compared to the traditional technique of having multiple processes compete to receive datagrams on the same socket.

從文檔中可以看到,該參數(shù)允許多個socket綁定到同一本地地址,即使socket是處于listen狀態(tài)的。


當多個listen狀態(tài)的socket綁定到同一地址時,各個socket的accept操作都能接受到新的tcp連接。


很神奇對吧,寫段代碼測試下:


#include #include #include #include #include #include #include #include
static int tcp_listen(char *ip, int port) { int lfd, opt, err; struct sockaddr_in addr;
lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); assert(lfd != -1);
opt = 1; err = setsockopt(lfd, SOL_SOCKET, SO_REUSEPORT,
本站聲明: 本文章由作者或相關機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
關閉
關閉