| ||||||
| 5/27 |
| 2002/12/31-2003/1/2 [Computer/SW/Languages] UID:26954 Activity:high |
12/31 When my program binds a socket to a port but dies due to an
error, how do I "unbind" the socket so I can run the program
again without getting "Address already in use"? Thanks.
\_ Man setsockopt, and look at SO_REUSEADDR
\_ You will probably need something like this:
int setReuseAddr(int listenfd) {
int on = 1, ret = 0;
if (setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,
(void *)&on,sizeof(on)) < 0) {
close(listenfd);
ret = -1;
}
return ret;
} |