NTP client connection timeout

Viewed 209
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(int argc, char **argv){
    struct addrinfo hints,*res;
    char hostname[12],timedata[200];
    
    memset(&hints,0,sizeof hints);
    hints.ai_family=AF_INET;
    hints.ai_socktype=SOCK_STREAM;
    hints.ai_flags=AI_PASSIVE;

    getaddrinfo(argv[1],"123",&hints,&res);
    int socketfd = socket(AF_INET,SOCK_STREAM,0);
    
    struct sockaddr_in *foo=(struct sockaddr_in *)res->ai_addr;
    printf("connecting to server: %s\n",inet_ntoa(foo->sin_addr));

    if(connect(socketfd,res->ai_addr,res->ai_addrlen)==-1){
        perror("connect error");return -1;}
    printf("connected to server\n");

    recv(socketfd,timedata,sizeof timedata,0);
    printf("%s\n",timedata);
    return 0;
}

this is my code for a ntp client that recieves the daytime message from ntp server

but when i run it ./ntpclient time.google.com i get connect error:Connection timed out i have tried other servers as well same problem

0 Answers
Related