我对套接字编程很陌生,而且我在调试以下代码时遇到了问题:
客户端:
#define SIZE sizeof(struct sockaddr_in)
int main(int argc, char* argv[]) {
printf("Starting setup\n");
int sockfd;
int adder;
int sum;
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(7013);
inet_pton(AF_INET, "127.0.0.1", &server.sin_addr.s_addr);
printf("Setup complete\n");
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket call failed.\n");
return EXIT_FAILURE;
}
else{
printf("socket: %i", sockfd);
}
//connect the socket
if (connect(sockfd, (struct sockaddr*) &server, SIZE) == -1) {
perror("Connect call failed");
return EXIT_FAILURE;
}
//Send and receive
while (!feof(stdin)) {
printf("Input an integer \n");
adder = atoi(fgets(intChar, 11, stdin));
if ((send(sockfd, &adder, 11, 0)) == -1) {
perror("Send error");
}
if (recv(sockfd, &sum, 1, 0) > 0) {
printf("%d", sum);
}
else {
printf("Server has died \n");
close(sockfd);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
服务器:
int sum = 0;
int numconnections = 0;
int main(int argc, char* argv[]) {
int sockfd;
int newsockfd;
int adder = 0;
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(7013);
server.sin_addr.s_addr = INADDR_ANY;
printf("Starting up. \n");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//Free port
int on = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (sockfd == -1) {
perror("Socket call failed. \n");
return EXIT_FAILURE;
}
//bind address
if (bind(sockfd, (struct sockaddr*) &server, sizeof(server)) == -1) {
perror("Bind call failed. \n");
return EXIT_FAILURE;
}
//listen
if (listen(sockfd, 5) == -1) {
perror("Listen call failed. \n");
return EXIT_FAILURE;
}
while (numconnections < 2) {
//accept a connection
printf("ready to accept connections\n");
if ((newsockfd = accept(sockfd, NULL, NULL)) == -1) {
perror("Accept call failed. \n");
continue;
}
printf("New connection detected");
numconnections++;
//spawn child to deal with it
if (fork() == 0) {
while (recv(newsockfd, &adder, 1, 0) > 0) {
printf("Sum is %d \n", sum);
printf("Adder is %d \n", adder);
sum += adder;
printf("Sending sum %d \n", sum);
send(newsockfd, &sum, 1, 0);
}
close(newsockfd);
return EXIT_SUCCESS;
}
}
close(newsockfd);
close(sockfd);
return EXIT_SUCCESS;
}
无论我是在同一台计算机上运行程序还是单独运行程序,当服务器收到套接字时,它会连续添加大约10次数字,几乎就像套接字正在循环一样。
从客户端提交3后,这是一些示例输出:
Starting up.
ready to accept connections
New connection detectedready to accept connections
New connection detectedSum is 0
Adder is 3
Sending sum 3
Sum is 3
Adder is 0
Sending sum 3
Sum is 3
Adder is 0
Sending sum 3
Sum is 3
Adder is 0
Sending sum 3
Sum is 3
Adder is 77
Sending sum 80
Sum is 80
Adder is 11
Sending sum 91
Sum is 91
Adder is 64
Sending sum 155
Sum is 155
Adder is 0
Sending sum 155
Sum is 155
Adder is 3
Sending sum 158
Sum is 158
Adder is 0
Sending sum 158
Sum is 158
Adder is 0
Sending sum 158
在客户端,我最初收到3,但后来我得到了一些无意义的数字,如4197123。
我有一种强烈的感觉,我错过了一些明显的东西。提前谢谢。
答案 0 :(得分:2)
while (recv(newsockfd, &adder, 1, 0) > 0) { ...
使用recv()的返回值它是实际收到的字节数。第三个论点也是错误的。它至少应该是sizeof(int)。或者你可以缓冲调用之间的部分接收,这是必要的,因为TCP没有消息边界:流可以在任何时候被分解。 (参见我的草图缓冲here)
您还应该更改send()的第三个参数; send()
类似。
adder = atoi(fgets(intChar, 11, stdin));
if ((send(sockfd, &adder, 11, 0)) == -1) { ...
(客户端)也是非常错误的,你发送的是int,而不是字符串。
最后:不要使用feof()
,至少不要这样。 feof()将在 fgets()失败后返回非零,NULL参数可能已经全部包含atcrash atoi()。