#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h >
int main(void)
{
int sockfd = 0, n = 0, ii = 1;
char recvBuff[1024];
struct sockaddr_in serv_addr;
setvbuf(stdout, NULL, _IONBF, 0);
printf("Retrieving time, please wait ");
memset(recvBuff, '0',sizeof(recvBuff));
n=-1;
while(n<=0)
{
// printf("====== attempt %d \n\n", ii);
printf(".");
if (ii == 50)
{
printf("\n");
ii =0;
}
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
serv_addr.sin_addr.s_addr = inet_addr("64.90.182.55");
// serv_addr.sin_addr.s_addr = inet_addr("96.47.67.105");
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(13);
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
n = read(sockfd, recvBuff, sizeof(recvBuff)-1);
close(sockfd);
usleep(10000);
ii++;
}
recvBuff[n] = 0;
printf("%s\n",recvBuff);
return 0;
}
Windows version
//============================================================================
// Name : windows_time_client.cpp
// Author : Norma Hermawan
// Version :
// Copyright : Your copyright notice
// Description : CONNECT TO REMOTE HOST (CLIENT APPLICATION)
// Include the needed header files.
// Don’t forget to link libws2_32.a to your program as well
// 2. To set the library
// Bring up the properties box for the project.
// Select all configurations.
// In the linker section on the left handside - select Input and in the right hand side add
// ws2_32.lib in the Additional Dependencies box
//============================================================================
#include "stdafx.h"
#include <winsock.h>
#include <iostream>
using namespace std;
#define TIMESERV_PORT 13
//#define TIMESERV_IP "96.47.67.105" // this is time server IP
#define TIMESERV_IP "64.90.182.55" // this is time server IP
//#define TIMESERV_IP "128.138.188.172" // this is time server IP
//#define TIMESERV_IP "211.233.40.78" // this is time server IP
//#define TIMESERV_IP "203.160.128.178" // this is time server IP
//#define TIMESERV_IP "113.20.31.30" // this is time server IP
int main() {
SOCKET sfd; //Socket handle
char buffer[80];
int n, ii =1;
time_t prev_stamp;
printf("Retrieving time, please wait ",ii);
do{
// system ("ping 64.90.182.55");
//Start up Winsock…
// printf("Starting winsock ============================= attempt %d\n\n",ii);
// printf("Retrieving time, attempt %d\n\n",ii);
printf(".");
if (ii==50){
printf("\n");
ii = 0;
}
WSADATA wsadata;
int error = WSAStartup(0x0202, &wsadata);
//Fill out the information needed to initialize a socket…
SOCKADDR_IN addr; //Socket address information
addr.sin_family = AF_INET; // address family Internet
addr.sin_port = htons (TIMESERV_PORT); //Port to connect on
addr.sin_addr.s_addr = inet_addr (TIMESERV_IP); //Target IP
// printf("Create socket\n");
sfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
// printf("Establishing Connection \n");
int res = connect(sfd, (sockaddr*) &addr, sizeof(addr));
// printf("Connectition is established \n");
memset(buffer, 0, sizeof(buffer)); //Clear the buffer
// printf("Receiving signal \n\n");
prev_stamp = time(NULL);
//Put the incoming text into our buffer
n = recv (sfd, buffer, sizeof(buffer)-1, 0);//) <= 0) &&((time(NULL) - prev_stamp) < 5))
closesocket(sfd);
WSACleanup(); //Clean up Winsock
// Sleep(10);
ii++;
}
while(n<=0);
printf("\nTime: ", n);
buffer[n] = 0;
printf("%s\n",buffer);
system("Pause");
return 0;
}