socket编程,想将几个客户端,绑定到同一个端口上,第一个可以绑定成功,第二个bind返回错误,errno=98(address already in use),我在socket选项里已经加上了,地址可以复用选项:
int optval=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char*)&optval,sizeof(optval));
可为啥还是报错呢,等高手给指点一下
...全文
8756打赏收藏
socket编程端口复用问题
socket编程,想将几个客户端,绑定到同一个端口上,第一个可以绑定成功,第二个bind返回错误,errno=98(address already in use),我在socket选项里已经加上了,地址可以复用选项: int optval=1; setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char*)&optval,sizeof(optval)); 可为啥还是报错呢,等高手给指点一下
What exactly does SO_REUSEADDR do?
This socket option tells the kernel that even if this port is busy (in
the TIME_WAIT state), go ahead and reuse it anyway. If it is busy,
but with another state, you will still get an address already in use
error. It is useful if your server has been shut down, and then
restarted right away while sockets are still active on its port. You
should be aware that if any unexpected data comes in, it may confuse
your server, but while this is possible, it is not likely.