【发布时间】:2021-11-30 07:02:39
【问题描述】:
我是socket新手,我正在尝试向服务器发送消息,如果服务器在5秒内没有收到来自客户端的另一条消息,则向客户端发送警告,否则合并两条消息并发送回给客户。
我正在使用 select,一旦调用 select(),服务器就无法接收第二条消息,它总是超时。
我做错了什么??
服务器
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <unistd.h>
#define BUF_SIZE 256
char *concat(const char *s1, const char *s2);
int main(int argc, char const *argv[]) {
struct sockaddr_in server, client;
struct timeval tv;
int sock, readSize, fd = 0;
char buf[BUF_SIZE], stringA[BUF_SIZE], stringB[BUF_SIZE];
socklen_t addressSize;
fd_set readfds;
bzero(&server, sizeof(server));
server.sin_family = PF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(6000);
// TCP check
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("socket: %s\n", strerror(errno));
return 1;
}
// Handle binding error
if (bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
printf("bind: %s\n", strerror(errno));
return 1;
}
// Handle connection error
if (listen(sock, 5) == -1) {
printf("listen: %s\n", strerror(errno));
return 1;
}
// Handle client acceptance error
addressSize = sizeof(client);
sock = accept(sock, (struct sockaddr *)&client, &addressSize);
while ((readSize = recv(sock, stringA, sizeof(stringA), 0))) {
stringA[readSize] = '\0';
printf("Read Message A: %s", stringA);
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
FD_SET(0, &readfds);
tv.tv_sec = 5;
tv.tv_usec = 0;
if (select(sock + 1, &readfds, NULL, NULL, &tv) < 0) {
printf("ERROR in select");
}
char *result;
// If more messgae receve within 5 seconds, but the program never reached this part
if (FD_ISSET(sock, &readfds)) {
// Get string B
readSize = recv(sock, stringB, sizeof(stringB), 0);
stringB[readSize] = '\0';
result = concat(stringA, stringB);
printf("Some more input received\n");
} else {
printf("Time out\n");
}
send(sock, &result, sizeof(result), 0);
}
printf("Client has closed the connection.\n");
close(sock);
return 0;
}
char *concat(const char *s1, const char *s2) {
char *result = malloc(strlen(s1) + strlen(s2) + 1);
strcpy(result, s1);
strcat(result, s2);
return result;
}
客户
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#define BUF_SIZE 256
int main(int argc, char const *argv[]) {
struct sockaddr_in server;
struct timeval tv;
int sock, readSize, addressSize;
char buf[BUF_SIZE];
bzero(&server, sizeof(server));
server.sin_family = PF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(6000);
sock = socket(PF_INET, SOCK_STREAM, 0);
addressSize = sizeof(server);
// TCP check
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("socket: %s\n", strerror(errno));
return 1;
}
// Handle connection error
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
printf("connect: %s\n", strerror(errno));
return 1;
}
while (1) {
fgets(buf, 256, stdin);
if (feof(stdin)) break;
send(sock, &buf, sizeof(buf), 0);
readSize = recv(sock, buf, sizeof(buf), 0);
buf[readSize] = '\0';
printf("%s\n", buf);
}
close(sock);
return 0;
}
【问题讨论】:
-
FD_ISSET(sock + 1, &readfds)为什么会有+1?这永远不会是真的。