C时间程序(NTP)不绑定套接字

时间:2012-10-11 16:57:53

标签: c sockets ntp

我写了一个应该从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);
}

1 个答案:

答案 0 :(得分: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);
}