-1
我正在尝试使用套接字编程在c中简单的客户端服务器程序。当我首先执行服务器然后执行客户端时,程序工作正常,但在两到三次成功执行后,客户端停止连接到服务器。如果我在一段时间后执行程序,这又开始工作。客户端在2-3次成功连接后停止连接到服务器。 socket编程在c
客户机代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket,len;
char buffer[1024];
struct sockaddr_in sadd;
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
sadd.sin_family = AF_INET;
sadd.sin_port = htons(12345);
sadd.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(sadd.sin_zero, '\0', sizeof sadd.sin_zero);
len = sizeof sadd;
while(connect(clientSocket, (struct sockaddr *) &sadd, len) == -1) {
sleep(1);
printf("trying again..\n");
}
read(clientSocket, buffer, sizeof(buffer));
printf("Data received: %s",buffer);
return 0;
}
服务器代码:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int defSock, newSock, len;
char buffer[1024];
struct sockaddr_in sadd, cadd;
socklen_t addr_size;
defSock = socket(PF_INET, SOCK_STREAM, 0);
sadd.sin_family = AF_INET;
sadd.sin_port = htons(12345);
sadd.sin_addr.s_addr = INADDR_ANY;
bind(defSock, (struct sockaddr *) &sadd, sizeof(sadd));
if(listen(defSock,5)==0)
printf("Listening\n");
else
printf("Error\n");
len = sizeof(cadd);
newSock = accept(defSock, (struct sockaddr *) &cadd, &len);
strcpy(buffer,"Hello World\n");
int n = write(newSock,buffer,sizeof(buffer));
close(newSock);
return 0;
}
在终端多次执行客户端:
[[email protected] Downloads]$ gcc client.c -o client
client.c: In function ‘main’:
client.c:16:25: warning: implicit declaration of function ‘inet_addr’ [-Wimplicit-function-declaration]
sadd.sin_addr.s_addr = inet_addr("127.0.0.1");
^
client.c:21:3: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
sleep(1);
^
client.c:25:2: warning: implicit declaration of function ‘read’ [-Wimplicit-function-declaration]
read(clientSocket, buffer, sizeof(buffer));
^
[[email protected] Downloads]$ ./client
Data received: Hello World
[[email protected] Downloads]$ ./client
trying again..
trying again..
trying again..
trying again..
trying again..
trying again..
trying again..
trying again..
trying again..
^C
[[email protected] Downloads]$ ./client
trying again..
trying again..
trying again..
^C
[[email protected] Downloads]$ ./client
trying again..
trying again..
trying again..
trying again..
^C
[[email protected] Downloads]$
另一终端上同时执行的服务器:
[[email protected] Downloads]$ gcc server.c -o server
server.c: In function ‘main’:
server.c:31:10: warning: implicit declaration of function ‘write’ [-Wimplicit-function-declaration]
int n = write(newSock,buffer,sizeof(buffer));
^
server.c:33:2: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
close(newSock);
^
[[email protected] Downloads]$ ./server
Listening
[[email protected] Downloads]$ ./server
Listening
^C
[[email protected] Downloads]$ ./server
Listening
^C
[[email protected] Downloads]$ ./server
Listening
^C
[[email protected] Downloads]$
这些警告的意思是你错过了一些'#include'线。首先解决它。 – Barmar
你可以尝试通过'setsockopt()'在'defSock'上打开'SO_REUSEADDR'套接字选项。你也可以考虑让服务器循环,以便通过同一个监听套接字(串行)为多个连接提供服务。 –
@JohnBollinger,非常感谢!添加'setsockopt(defSock,SOL_SOCKET,SO_REUSEADDR,&(int){1},sizeof(int));'使代码生效。 –