socket程序退出后有时候端口还没有释放?请问高人如何解决啊。正确立马结帖。
来源: 互联网 发布时间:2015-07-14
本文导语: 处理如下信号,然后在sig_exit中close掉socket.但有时候端口没有释放掉。 signal(SIGINT, sig_exit); /*设置中断退出*/ signal(SIGHUP, sig_exit); signal(SIGQUIT, sig_exit); signal(SIGABRT, sig_exit); signal(SIGKILL, sig_exit); ...
处理如下信号,然后在sig_exit中close掉socket.但有时候端口没有释放掉。
signal(SIGINT, sig_exit); /*设置中断退出*/
signal(SIGHUP, sig_exit);
signal(SIGQUIT, sig_exit);
signal(SIGABRT, sig_exit);
signal(SIGKILL, sig_exit);
signal(SIGTERM, sig_exit);
signal(SIGCHLD, SIG_IGN );
signal(SIGSTOP, sig_exit);
signal(SIGXCPU, sig_exit);
signal(SIGPROF, sig_exit);
signal(SIGWINCH, sig_exit);
signal(SIGIO, SIG_IGN );
signal(SIGPWR, sig_exit);
signal(SIGILL, sig_exit);
signal(SIGSEGV, sig_exit);
signal(SIGINT, sig_exit); /*设置中断退出*/
signal(SIGHUP, sig_exit);
signal(SIGQUIT, sig_exit);
signal(SIGABRT, sig_exit);
signal(SIGKILL, sig_exit);
signal(SIGTERM, sig_exit);
signal(SIGCHLD, SIG_IGN );
signal(SIGSTOP, sig_exit);
signal(SIGXCPU, sig_exit);
signal(SIGPROF, sig_exit);
signal(SIGWINCH, sig_exit);
signal(SIGIO, SIG_IGN );
signal(SIGPWR, sig_exit);
signal(SIGILL, sig_exit);
signal(SIGSEGV, sig_exit);
|
{
int opt = 1;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
/* Set port reuse */
if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(opt))
int opt = 1;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
/* Set port reuse */
if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(opt))