Skip to content

Instantly share code, notes, and snippets.

@tanayseven
Created August 25, 2015 20:34
Show Gist options
  • Save tanayseven/7097b31cc079ac5cc58e to your computer and use it in GitHub Desktop.
Save tanayseven/7097b31cc079ac5cc58e to your computer and use it in GitHub Desktop.
Time Server
/*
The MIT License (MIT)
Copyright (c) 2015 Tanay PrabhuDesai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define ERROR -1
#define PORT 29008
#define LISTEN_BACKLOG 1
#define CONN_ADDR "127.0.0.1"
#define MAX_STR_LEN 2048
int main(void)
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
time_t start_time, rtt, current_time;
if (sfd == ERROR) {
}
char ip[20];int port;
struct sockaddr_in server_addr;
printf("Enter the ip address: ");
scanf("%s",ip);
printf("Enter the port number: ");
scanf("%d",&port);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(port);
if (connect(sfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == ERROR ) {
}
fflush(stdout);
fflush(stdin);
start_time = time(NULL);
read(sfd,&current_time, sizeof(current_time));
rtt = time(NULL) - start_time;
current_time += rtt / 2;
printf("Time is: %s\n", ctime(&current_time));
close(sfd);
return 0;
}
/*
The MIT License (MIT)
Copyright (c) 2015 Tanay PrabhuDesai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define ERROR -1
#define CONN_ADDR "127.0.0.1"
#define PORT 29008
int main(void) {
int fd = socket(AF_INET, SOCK_STREAM, 0), cfd;
time_t current_time;
pid_t child;
if (fd == ERROR) {
}
struct sockaddr_in my_addr;
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = inet_addr(CONN_ADDR); //accept any IP address communication
my_addr.sin_port = htons(PORT);
if (bind(fd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == ERROR) {
}
if (listen(fd, 5) == ERROR ) {
}
printf("Listening on: %s:%d\n", CONN_ADDR, PORT);
while (1) {
cfd = accept(fd, (struct sockaddr *) NULL, NULL);
child = fork();
if (child == ERROR) {
} else if ( child == 0 ) {
if ( cfd == ERROR ) {
}
fflush(stdout);
fflush(stdin);
printf("A client requested for time:\t");
current_time = time(NULL);
printf("Time is: %s\n", ctime(&current_time));
write(cfd, &current_time, sizeof(current_time));
break;
}
}
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment