日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 嵌入式 > Linux閱碼場
[導(dǎo)讀]當(dāng)然可以,只要你使用?SO_REUSEPORT?這個(gè)參數(shù)。還是先來看下man文檔中是怎么說的:SO_REUSEPORT(sinceLinux3.9)PermitsmultipleAF_INETorAF_INET6socketstobeboundtoanidenticalsocke...

當(dāng)然可以,只要你使用?SO_REUSEPORT?這個(gè)參數(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ù)允許多個(gè)socket綁定到同一本地地址,即使socket是處于listen狀態(tài)的。


當(dāng)多個(gè)listen狀態(tài)的socket綁定到同一地址時(shí),各個(gè)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,
本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除。
關(guān)閉