/*
 * Connect any NTP-Pool server to query the current time.
 */
static void QueryDateAndTime(void)
{
    uint32_t ip;
    time_t now;
    int i;

    /* First connect the DNS server to get the IP address. */
    printf("Is There Anybody Out There? ");
    ip = NutDnsGetHostByName((uint8_t *) "pool.ntp.org");
    if (ip == 0) {
        puts("No?");
    } else {
        /* We do have an IP, now try to connect it. */
        printf("Yes, found %s\n", inet_ntoa(ip));
        for (i = 0; i < 3; i++) {
            printf("What's the time? ");
            if (NutSNTPGetTime(&ip, &now) == 0) {
                /* We got a valid response, display the result. */
                printf("%s GMT\n", Rfc1123TimeString(gmtime(&now)));
                return;
            } else {
                /* It failed. May be the server is too busy. Try
                   again in 5 seconds. */
                puts("Sorry?");
                NutSleep(5000);
            }
        }
    }
    /* We give up after 3 trials. */
    puts("You missed the starting gun.");
}
Esempio n. 2
0
int TcpHostConnect(TCPSOCKET *sock, const char * host, uint16_t port)
{
    uint32_t ip;

    /* Get remote host IP address. */
    ip = inet_addr(host);
    if (ip == (uint32_t)-1 || ip == 0) {
        /* Doesn't look like an address, try host name. */
        ip = NutDnsGetHostByName((uint8_t *)host);
        if (ip == 0) {
            /* Give up. */
            return -1;
        }
    }
    /* Got a valid address, connect to it. */
    return NutTcpConnect(sock, ip, port);
}