gpt4 book ai didi

C UDP 客户端服务器不工作

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:46 25 4
gpt4 key购买 nike

所以我有一个 UDP 服务器,它使用以下代码将小时或日期返回给客户端:

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <time.h>

void main(int argc, char *argv[]) {
struct addrinfo hints;
struct addrinfo *res;
struct sockaddr_storage cli;
time_t rawtime;
struct tm* timeinfo;
char tbuffer[9];
char buf[81], host[NI_MAXHOST], serv[NI_MAXSERV];

hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */

getaddrinfo(argv[1], argv[2], &hints, &res);
int sd = socket(res->ai_family, res->ai_socktype, 0);
bind(sd, (struct sockaddr *)res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);

while(1) {
socklen_t clen = sizeof(cli);
int c = recvfrom(sd, buf, 80, 0, (struct sockaddr*) &cli, &clen);
buf[c] = '\0';
getnameinfo((struct sockaddr*) &cli, clen, host, NI_MAXHOST, serv, NI_MAXSERV, NI_NUMERICHOST);
time(&rawtime);
timeinfo = localtime(&rawtime);
if(buf[0] == 't') {
printf("%ld bytes de %s:%s\n", c, host, serv);
ssize_t chars = strftime(tbuffer, sizeof(tbuffer), "%T", timeinfo);
sendto(sd, tbuffer, chars, 0, (struct sockaddr *)&cli, clen);
} else if(buf[0] == 'd') {
printf("%ld bytes de %s:%s\n", c, host, serv);
ssize_t chars = strftime(tbuffer, sizeof(tbuffer), "%D", timeinfo);
sendto(sd, tbuffer, chars, 0, (struct sockaddr *)&cli, clen);
} else if(buf[0] == 'q') {
printf("Saliendo...\n");
exit(EXIT_SUCCESS);
} else {
printf("Comando no soportado %s", buf);
}
}
}

为了测试我使用的代码 ./server <IPServerAddres> <PortAddress>在服务器端和nc -u <IpServerAddress> <PortServer>在客户端(另一台机器)。

现在我想创建 UDP 客户端 UDP 并在不使用 nc 的情况下执行相同操作, 只写 ./client <IPServerAddres> <PortAddress> <command>其中 command可以是t时间d日期和 q退出,就像在服务器中一样。

客户端程序代码如下:

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <time.h>

int main(int argc, char *argv[]) {
struct addrinfo hints;
struct addrinfo *res, *resp;
int sd, j, s;
size_t len;
ssize_t nread, nwrite;
char buf[500];

if (argc < 3) {
fprintf(stderr, "Usage: %s host port command...\n", argv[0]);
exit(EXIT_FAILURE);
}

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */

getaddrinfo(argv[1], argv[2], &hints, &res);

for(resp = res; resp != NULL; resp = resp->ai_next) {
sd = socket(resp->ai_family, resp->ai_socktype, resp->ai_protocol);
if(sd == -1) {
perror("socket()");
continue;
}

if(connect(sd, resp->ai_addr, resp->ai_addrlen) == -1) {
perror("socket()");
} else {
break;
}

close(sd);
}

freeaddrinfo(res);

for(j = 3; j < argc; j++) {
len = strlen(argv[j]) + 1;
nwrite = write(sd, argv[j], len);
if(nwrite == -1) perror("write()");
nread = read(sd, buf, 500);
buf[nread] = 0;
if(nread == -1) perror("read()");
printf("Recibidos %ld bytes: %s\n", (long) nread, buf);
}

exit(EXIT_SUCCESS);
}

问题是,当我尝试运行我的客户端程序时,我没有得到任何响应,所以我猜是在无限循环或类似的情况下。

如有任何帮助,我将不胜感激,我正在学习 C,所以对于错误,我深表歉意,谢谢。

已编辑:更正了 breakfor以防循环 connect()返回不同于 -1 的内容。

最佳答案

问题在于您计算要发送的消息的长度:

len = strlen(argv[j] + 1);

这会获取字符串的长度从第二个字符开始。你想要:

len = strlen(argv[j]) + 1;

这将为您提供字符串长度加 1 的空终止符。

此外,在阅读响应后,您需要对返回的内容进行空终止:

buf[nread] = 0;

关于C UDP 客户端服务器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51107432/

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