SOCKET slisten[16];
char *szPort="5150";
struct addrinfo  hints,
* res=NULL,
* ptr=NULL;
int count=0,
rc;memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
rc = getaddrinfo(NULL, szPort, &hints, &res);
if (rc != 0) {
// failed for some reason
}
ptr = res;
while (ptr)
{
slisten[count] = socket(ptr->ai_family, 
ptr->ai_socktype, ptr->ai_protocol);
if (slisten[count] == INVALID_SOCKET) {
// socket failed
}
rc = bind(slisten[count], ptr->ai_addr, ptr->ai_addrlen);
if (rc == SOCKET_ERROR) {
// bind failed
}
rc = listen(slisten[count], 7);
if (rc == SOCKET_ERROR) {
// listen failed
}
count++;
ptr = ptr->ai_next;
}上面的代码是不是在同一个端口上建立多个监听套接字?
这样做有什么好处?
能提高服务器的并发率和吞吐量?多网卡的服务器就有多个IP,这和每个服务器一个IP,只创建一个监听套接字有什么区别?