socket编程小问题:地址已经被使用——Address already in use

时间:2022-02-06 23:56:20

转自:http://blog.csdn.net/a_tu_/article/details/44625117

很多socket编程的初学者可能会遇到这样的问题:如果先ctrl+c结束服务器端程序的话,再次启动服务器就会出现Address already in use这个错误,或者你的程序在正常关闭服务器端socket后还是有这个问题。正如下面的这段简单的socket程序。

server.cpp

 1 #include <sys/types.h>   
2 #include <sys/socket.h>
3 #include <stdio.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8
9 #define BUFFER_SIZE 40
10
11 int main()
12 {
13 char buf[BUFFER_SIZE];
14 int server_sockfd, client_sockfd;
15 int sin_size=sizeof(struct sockaddr_in);
16 struct sockaddr_in server_address;
17 struct sockaddr_in client_address;
18 memset(&server_address,0,sizeof(server_address));
19 server_address.sin_family = AF_INET;
20 server_address.sin_addr.s_addr = INADDR_ANY;
21 server_address.sin_port = htons(12000);
22 // 建立服务器端socket
23 if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
24 {
25 perror("server_sockfd creation failed");
26 exit(EXIT_FAILURE);
27 }
28 // 将套接字绑定到服务器的网络地址上
29 if((bind(server_sockfd,(struct sockaddr *)&server_address,sizeof(struct sockaddr)))<0)
30 {
31 perror("server socket bind failed");
32 exit(EXIT_FAILURE);
33 }
34 // 建立监听队列
35 listen(server_sockfd,5);
36 // 等待客户端连接请求到达
37 client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,(socklen_t*)&sin_size);
38 if(client_sockfd<0)
39 {
40 perror("accept client socket failed");
41 exit(EXIT_FAILURE);
42 }
43 // 接收客户端数据
44 if(recv(client_sockfd,buf,BUFFER_SIZE,0)<0)
45 {
46 perror("recv client data failed");
47 exit(EXIT_FAILURE);
48 }
49 printf("receive from client:%s/n",buf);
50 // 发送数据到客户端
51 if(send(client_sockfd,"I have received your message.",BUFFER_SIZE,0)<0)
52 {
53 perror("send failed");
54 exit(EXIT_FAILURE);
55 }
56 close(client_sockfd);
57 close(server_sockfd);
58 exit(EXIT_SUCCESS);
59 }

 

client.cpp

 1 #include <sys/types.h>   
2 #include <sys/socket.h>
3 #include <stdio.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8
9 #define BUFFER_SIZE 40
10
11 int main()
12 {
13 char buf[BUFFER_SIZE];
14 int client_sockfd;
15 int len;
16 struct sockaddr_in address;// 服务器端网络地址结构体
17 int result;
18 client_sockfd = socket(AF_INET, SOCK_STREAM, 0);// 建立客户端socket
19 address.sin_family = AF_INET;
20 address.sin_addr.s_addr = inet_addr("127.0.0.1");
21 address.sin_port = htons(12000);
22 len = sizeof(address);
23 // 与远程服务器建立连接
24 result = connect(client_sockfd, (struct sockaddr *)&address, len);
25 if(result<0)
26 {
27 perror("connect failed");
28 exit(EXIT_FAILURE);
29 }
30 printf("Please input the message:");
31 scanf("%s",buf);
32 send(client_sockfd,buf,BUFFER_SIZE,0);
33 recv(client_sockfd,buf,BUFFER_SIZE,0);
34 printf("receive data from server: %s/n",buf);
35 close(client_sockfd);
36 return 0;
37 }

在成功的运行了第一次之后,当你再次启动服务器端程序时,./server就变得邪恶起来,在bind()这个函数中居然出现了Address already in use这个错误。

                                  socket编程小问题:地址已经被使用——Address already in use

 然后你开始迷惑了,难道是忘记将socket给关闭了,或是关闭socket的顺序不对?经过种种猜测与试验,你发现问题毫无进展......过了一会,当你再次抱着试试看的态度重新在Linux的“黑色终端”中输入./server时,程序居然运行了,什么情况?究其原因,是socket选项在捣鬼。下面是IBM官网上对这一情况的具体解释,参见http://www.ibm.com/developerworks/cn/linux/l-sockpit/

bind 普遍遭遇的问题是试图绑定一个已经在使用的端口。该陷阱是也许没有活动的套接字存在,但仍然禁止绑定端口(bind 返回 EADDRINUSE),它由 TCP 套接字状态 TIME_WAIT 引起。该状态在套接字关闭后约保留 2 到 4 分钟。在 TIME_WAIT 状态退出之后,套接字被删除,该地址才能被重新绑定而不出问题。

等待 TIME_WAIT 结束可能是令人恼火的一件事,特别是如果您正在开发一个套接字服务器,就需要停止服务器来做一些改动,然后重启。幸运的是,有方法可以避开 TIME_WAIT 状态。可以给套接字应用 SO_REUSEADDR 套接字选项,以便端口可以马上重用。

考虑清单 3 的例子。在绑定地址之前,我以 SO_REUSEADDR 选项调用 setsockopt。为了允许地址重用,我设置整型参数(on)为 1 (不然,可以设为 0 来禁止地址重用)。

      按照IBM的做法,我重新改写了server.c的代码。

server.c

 1 #include <sys/types.h>   
2 #include <sys/socket.h>
3 #include <stdio.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8
9 #define BUFFER_SIZE 40
10
11 int main()
12 {
13 char buf[BUFFER_SIZE];
14 int server_sockfd, client_sockfd;
15 int sin_size=sizeof(struct sockaddr_in);
16 struct sockaddr_in server_address;
17 struct sockaddr_in client_address;
18 memset(&server_address,0,sizeof(server_address));
19 server_address.sin_family = AF_INET;
20 server_address.sin_addr.s_addr = INADDR_ANY;
21 server_address.sin_port = htons(12000);
22 // 建立服务器端socket
23 if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
24 {
25 perror("server_sockfd creation failed");
26 exit(EXIT_FAILURE);
27 }
28 // 设置套接字选项避免地址使用错误
29 int on=1;
30 if((setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))<0)
31 {
32 perror("setsockopt failed");
33 exit(EXIT_FAILURE);
34 }
35 // 将套接字绑定到服务器的网络地址上
36 if((bind(server_sockfd,(struct sockaddr *)&server_address,sizeof(struct sockaddr)))<0)
37 {
38 perror("server socket bind failed");
39 exit(EXIT_FAILURE);
40 }
41 // 建立监听队列
42 listen(server_sockfd,5);
43 // 等待客户端连接请求到达
44 client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,(socklen_t*)&sin_size);
45 if(client_sockfd<0)
46 {
47 perror("accept client socket failed");
48 exit(EXIT_FAILURE);
49 }
50 // 接收客户端数据
51 if(recv(client_sockfd,buf,BUFFER_SIZE,0)<0)
52 {
53 perror("recv client data failed");
54 exit(EXIT_FAILURE);
55 }
56 printf("receive from client:%s/n",buf);
57 // 发送数据到客户端
58 if(send(client_sockfd,"I have received your message.",BUFFER_SIZE,0)<0)
59 {
60 perror("send failed");
61 exit(EXIT_FAILURE);
62 }
63 close(client_sockfd);
64 close(server_sockfd);
65 exit(EXIT_SUCCESS);
66 }

这次,让我们再次反复的启动服务器,尽情的在“黑窗户”里面输入./server ./server ./server ......服务器的程序好像突然间变乖了,呵呵,童鞋们,为自己的成就庆祝吧!!!