1

我写了一个小程序,应该从 SNTP 服务器获取时间。遇到程序没有绑定套接字的问题。我阅读了 RFC 2030,我正在按照它的解释做所有事情,使用 UDP,端口 123。还仔细检查了 UDP 不需要连接,只需要绑定。我看不出我的错,调试器也没有给我有用的信息。

这是我的代码:

#include <stdio.h>
#include <winsock2.h>
#include <winsock.h>
#include <Ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib" )
#define BUFFSIZE 1024

int main()
{
    WSADATA wsaData;
    int sockfd;
    char msg[48];
    unsigned long buffer[BUFFSIZE];
    int rv;
    int counter = 0;
    int numbytes;
    struct addrinfo hints, *servinfo, *p;
    struct sockaddr_storage their_addr;
    socklen_t addr_size;

    for(counter = 0; counter < 48; counter++)
        msg[counter] = 0;

    msg[0] = 11; //1 byte = 3 first flags set in binary: 00 001 011
    msg[1] = 0;
    msg[2] = 6;
    msg[3] = 1;
    msg[12] = 76;
    msg[13] = 79;
    msg[14] = 67;
    msg[15] = 76;


    if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) 
    {
        fprintf(stderr, "WSAStartup failed.\n");
        exit(1);
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM; // Use UDP

    if ((rv = getaddrinfo("ntp.belnet.be", "123", &hints, &servinfo)) != 0) {
        fprintf(stderr, "Getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }
    // 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("Failed to create the socket\n");
            continue;
        }
        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            closesocket(sockfd);
            continue;
        }
        break;
    }
    if (p == NULL) {
        fprintf(stderr, "Failed to bind socket\n");
        return 2;
    }

    freeaddrinfo(servinfo);

    //zenden en vragen naar de tijd
    if ((numbytes = sendto(sockfd, msg, strlen(msg), 0,p->ai_addr, p->ai_addrlen)) == -1) {
        perror("Couldn't send the message.\n");
        exit(1);
    }

    addr_size = sizeof their_addr;
    if ((numbytes = recvfrom(sockfd, (char *)buffer, 12 * sizeof(buffer[0]) , 0,(struct sockaddr *)&their_addr, &addr_size))== -1) {
        perror("Failed Receive");
        exit(1);
    }

    closesocket(sockfd); //close the socket
    WSACleanup();
    return 0;
}

有人能帮我吗?我究竟做错了什么?

编辑:已经发现这里的问题是解决方案:

if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) 
{
    fprintf(stderr, "WSAStartup failed.\n");
    exit(1);
}

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; // Use UDP

if ((rv = getaddrinfo("ntp.belnet.be", "123", &hints, &servinfo)) != 0) {
    fprintf(stderr, "Getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
}
// loop door al de resultaten en maak de socket
for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
        perror("Failed to create the socket\n");
        continue;
    }
    break;
}
//zenden en vragen naar de tijd
if ((numbytes = sendto(sockfd, msg, 48, 0,p->ai_addr, p->ai_addrlen)) == -1) {
    perror("Couldn't send the message.\n");
    exit(1);
}
addr_size = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, (char *)buffer, 48 , 0,(struct sockaddr *)&their_addr, &addr_size))== -1) {
    perror("Failed Receive");
    exit(1);
}
4

1 回答 1

0
if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) 
{
    fprintf(stderr, "WSAStartup failed.\n");
    exit(1);
}

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; // Use UDP

if ((rv = getaddrinfo("ntp.belnet.be", "123", &hints, &servinfo)) != 0) {
    fprintf(stderr, "Getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
}
// loop door al de resultaten en maak de socket
for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
        perror("Failed to create the socket\n");
        continue;
    }
    break;
}
//zenden en vragen naar de tijd
if ((numbytes = sendto(sockfd, msg, 48, 0,p->ai_addr, p->ai_addrlen)) == -1) {
    perror("Couldn't send the message.\n");
    exit(1);
}
addr_size = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, (char *)buffer, 48 , 0,(struct sockaddr *)&their_addr, &addr_size))== -1) {
    perror("Failed Receive");
    exit(1);
}
于 2012-10-11T17:19:39.907 回答