我知道你会将其视为重复(question1,question2,question3),但答案并不是我所寻找的(而且我认为也是其他人) )。
所以,我指的是socket master(我爱你们):如果我关闭套接字怎么能得到绑定错误(地址已经在使用中)?
我会描述我的问题。
我有一个与服务器进行通信的客户端 在服务器中,我有两个套接字:sockS(主要套接字,监听)和sockTX(客户端一个) 如果我打电话给我的程序一次,通讯很好,然后我关闭两个套接字
如果我回想起服务器和客户端,我会收到错误并且我要等待TIME_WAIT(在Ubuntu 32bit上大约3分钟)
为什么在关闭两个套接字后,我仍然会收到绑定错误?
有一种方法可以使它无任何魔法(SO_REUSEADDR)吗?
我知道我的代码中的某些内容是错误的...
谢谢大家。
代码:
客户
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 5000
#define SERVER "127.0.0.1"
#define MAXLINE 128
int printMessage(char* str);
int main(){
char buff[MAXLINE+1];
struct sockaddr_in server, client;
struct hostent *host;
int sock, n;
//socklen_t len;
if((sock = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("\nErrore socket()");
return -1;
}
client.sin_family = AF_INET;
client.sin_port = htons(0); // la porta e' scelta dal sistema operativo
client.sin_addr.s_addr = htonl(INADDR_ANY);
if( bind(sock,(struct sockaddr*)&client, sizeof(client)) == -1){
perror("\nErrore bind()");
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
if((host = gethostbyname(SERVER)) == NULL ){
perror("\nErrore gethostbyname()");
return -1;
}
server.sin_addr = *((struct in_addr *)host->h_addr);
if(connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0){
perror("\nErrore connect");
return -1;
}
// MESSAGGIO
sprintf(buff, "CTX\nclientTX\nserver\nCiao da client\n");
if(send(sock, buff, strlen(buff), 0) < 0) {
perror("\nErrore sendto");
return -1;
}
else {
printf("\nMessaggio inviato");
}
if((n = recv(sock, buff, MAXLINE, 0)) < 0) {
perror("\nErrore ricezione risposta");
return -1;
} else {
buff[n] = '\0';
int test = printMessage(buff);
printf("\nEsito: %s\n", (test == 1 ? "OK" : "FAIL"));
}
shutdown(sock, 2); // 2 = RD_WR
close(sock);
return 0;
}
int printMessage(char* str){
int i;
char* temp;
printf("Mittente: ");
for(i = 0; str[i] != '\n'; i++)
printf("%c", str[i]);
printf(" ");
for(i = i+1; str[i] != '\n'; i++)
printf("%c", str[i]);
printf("\nDestinatario: ");
for(i = i+1; str[i] != '\n'; i++)
printf("%c", str[i]);
temp = (char*)malloc(30 * sizeof(char));
strncpy(temp, str+i+1, 30);
printf("Messaggio: %s\n", temp);
if(strcmp(temp, "OK") == 0)
return 1;
else
return 0;
}
服务器:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 5000
#define SERVER "127.0.0.1"
#define MAXLINE 128
int printMessage(char* str);
int main() {
char buff[MAXLINE+1];
struct sockaddr_in server; //, client;
int sockS, sockTX, n;
if((sockS = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("\nErrore socket()");
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockS, (struct sockaddr *)&server, sizeof(server)) == -1)
{
perror("\nErrore bind()");
return -1;
}
if(listen(sockS, 10) == -1)
{
perror("\nErrore listen()");
return -1;
}
printf("SERVER\nInizializzazione completata!\n");
if((sockTX = accept(sockS, (struct sockaddr *)NULL, NULL)) == -1)
{
perror("\nErrore accept()");
return -1;
}
printf("Socket connesso\n");
// INVIO
if((n = recv(sockTX, buff, MAXLINE, 0)) < 0) {
perror("\nErrore recv()");
return -1; // BREAK??
} else {
buff[n] = '\0';
printMessage(buff);
}
sprintf(buff, "S\nserver\nclientTX\nOK\n");
if(send(sockTX, buff, strlen(buff), 0) < 0){
perror("\nErrore send()");
return -1; // BREAK??
} else {
printf("Risposta inviata\n");
}
shutdown(sockTX, 2);
close(sockTX);
shutdown(sockS, 2);
close(sockS);
return 0;
}
int printMessage(char* str){
int i;
char* temp;
printf("Mittente: ");
for(i = 0; str[i] != '\n'; i++)
printf("%c", str[i]);
printf(" ");
for(i = i+1; str[i] != '\n'; i++)
printf("%c", str[i]);
printf("\nDestinatario: ");
for(i = i+1; str[i] != '\n'; i++)
printf("%c", str[i]);
temp = (char*)malloc(30 * sizeof(char));
strncpy(temp, str+i+1, 30);
printf("Messaggio: %s\n", temp);
if(strcmp(temp, "OK\n") == 0)
return 1;
else
return 0;
}
谢谢大家
编辑1 :可能的解决方案(不是很漂亮,但只比SOCK_REUSEADDR多一点)
尝试在关闭和关闭两个服务器的套接字之前添加一个sleep(1)。
客户端将在服务器之前关闭,它将工作
我知道,不是很漂亮。
或者,更好的是,在关闭服务器内的第一个套接字之前,您可以检查客户端是否关闭了连接(如here)
答案 0 :(得分:6)
如果您遵循TCP状态机图,您将看到如果套接字启动发送FIN,则套接字必须转换到TIME-WAIT状态。在不等待客户端的FIN的情况下使用shutdown(sockTX, 2)
就是这样。
如果您希望服务器等待客户端的FIN,则先阻止recv()
等待0返回值。然后,您可以close()
。
请注意,除非您以某种方式复制了套接字(使用dup*()
或fork()
调用),否则如果立即跟踪shutdown()
则无需调用close()
由close()
。您只需调用shutdown()
(如果套接字已被复制,则仅在最后一个副本关闭时才会发送FIN。)
根本不需要sockS
接受套接字(while (recv(sockTX,...) > 0) {}
close(sockTX);
close(sockS);
)。
因此,我会将您的服务器端更改为类似以下内容以清理套接字:
{{1}}