Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C UDP客户端服务器不工作_C_Linux_Server_Udp_Client - Fatal编程技术网

C UDP客户端服务器不工作

C UDP客户端服务器不工作,c,linux,server,udp,client,C,Linux,Server,Udp,Client,因此,我有一个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

因此,我有一个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);
        }
    }
}
问题是,当我尝试运行我的客户机程序时,我没有得到响应,所以我猜是在一个无限循环或类似的循环中

任何帮助都将被感谢,我正在学习C,所以我为错误感到抱歉,谢谢


已编辑:更正了
for
循环中的
中断
,以防
connect()
返回与-1不同的值。

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

len = strlen(argv[j] + 1);
这将获取从第二个字符开始的字符串长度。你想要:

len = strlen(argv[j]) + 1;
这将为空终止符提供字符串长度加1

此外,在读取响应后,您需要null终止您得到的结果:

buf[nread] = 0;

检查
resp
for
循环之后是否为NULL。我添加了
if(resp==NULL){fprintf(stderr,“无法连接”);exit(exit_FAILURE);}
并得到错误“无法连接”,这是为什么?您需要查看从
getaddrinfo
返回的地址,如果有的话。您还可以检查
getaddrinfo
的返回代码,并将任何非零返回值传递给
gai_strerror
以获取错误字符串。我现在检查
getaddrinfo
的返回值,如果
for
循环出现错误,我已经更改了
break
的情况,如果
connect
返回的值不同于-1,则循环中断。在这些改变之后,当我运行程序时,我什么都没有得到,它似乎被卡在一个无限循环上…这是真的,谢谢,我已经改变了计算问题,缓冲区null终止,但它一直作为一个无限循环执行,我不知道为什么…我用客户端程序的最新版本编辑了帖子,以防你看到更多的东西。
buf[nread] = 0;