gpt4 book ai didi

c - UDP 套接字 - 服务器卡在 recvfrom 上

转载 作者:行者123 更新时间:2023-11-30 15:21:48 25 4
gpt4 key购买 nike

我正在尝试实现客户端-服务器网络。我正在关注Beej's Guide to Network Programming

我的程序扫描键盘输入,在数组中查找关联值,然后通过 UDP 向我的服务器发送 GET 请求。但是,我的服务器似乎没有收到我的请求。

这是我的客户端代码摘录:

    // loop through all the results and make a socket
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("talker: socket");
continue;
}

break;
}

if (p == NULL) {
fprintf(stderr, "talker: failed to bind socket\n");
return 2;
}

printf("Please Enter Your Search (USC, UCLA etc.):");
char searchKey[5];
fgets(searchKey, sizeof(searchKey), stdin);
int len, bytes_sent;
char value[8];
len = strlen(value);
char request[10];
for(int i = 0; i < sizeof(clientMap) / sizeof(struct Mapping); i++) {
if (strcmp(clientMap[i].key, searchKey) == 0) {
//value = clientMap[i].value;
memcpy(value, clientMap[i].value, sizeof(char) * 6);
strcat(request, "GET ");
strcat(request, value);
break;
}
}
printf("The Client 1 has received a request with search word %s, which maps to key %s\n", searchKey, value);
printf("The Client 1 sends the request %s to the Server 1 with port number %s and IP address %s\n", request, SERVERPORT, argv[1]);
printf("The Client1's port number is TODO and the IP address is TODO\n");
if ((numbytes = sendto(sockfd, request, strlen(request), 0,
p->ai_addr, p->ai_addrlen)) == -1) {
perror("talker: sendto");
exit(1);
}

freeaddrinfo(servinfo);

printf("talker: sent %d bytes to %s\n", numbytes, argv[1]);

这是我的服务器摘录:

// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("listener: socket");
continue;
}

if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("listener: bind");
continue;
}

break;
}

if (p == NULL) {
fprintf(stderr, "listener: failed to bind socket\n");
return 2;
}

freeaddrinfo(servinfo);

printf("listener: waiting to recvfrom...\n");

addr_len = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}

buf[numbytes] = '\0';
printf("The Server 1 has received a request with key %s\n", buf);
for(int i = 0; i < sizeof(serverMap) / sizeof(struct Mapping); i++) {
if (strcmp(serverMap[i].key, buf) == 0) {
char post[13];
strcpy(post, "POST ");
strcat(post, serverMap[i].value);
printf("The Server 1 sends the reply %s to TODO: Finish this!", post);
sendto(sockfd, post, strlen(post), 0, (struct sockaddr *)&their_addr, addr_len);
break;
}
}
close(sockfd);

我知道问题是我的服务器没有接收到数据报。我想我犯了一些明显很容易的错误,但我对 CNetworks 的理解并不是那么强。

顺便说一句,您可能会注意到我的打印语句包含TODO。我不确定如何获取这些IP 地址端口号。有人可以帮忙解决这个问题吗?

最佳答案

无论如何,您的服务器需要一个固定端口来监听。您如何期望您的客户端知道它应该发送到哪个端口?只需在服务器和客户端上使用任意端口即可。

由于您是新手,因此首先在本地尝试所有内容。服务器应该监听 INADDR_ANY 和您选择的任意端口。

关于c - UDP 套接字 - 服务器卡在 recvfrom 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29478002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com