0% found this document useful (0 votes)
5 views

Assignment 2

The document contains a C implementation of Berkeley's Clock Synchronization Algorithm, detailing both server and client code. The server collects time from multiple clients, calculates the average time difference, and updates the local time accordingly. The client sends its local time to the server and receives the updated time after synchronization.

Uploaded by

Hardik Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 2

The document contains a C implementation of Berkeley's Clock Synchronization Algorithm, detailing both server and client code. The server collects time from multiple clients, calculates the average time difference, and updates the local time accordingly. The client sends its local time to the server and receives the updated time after synchronization.

Uploaded by

Hardik Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program - 2

Aim: Implement Berkeley’s Clock Synchronization Algorithm.

Code:

====================Server Side====================

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <string.h>

#define SERVERPORT 8080


#define MAX_BUFF 1024
#define MAX_MESSAGE 30
#define MAX_CLIENTS 3

typedef struct sockaddr SA;


typedef struct sockaddr_in SA_IN;

int num_slaves = 0;
int flag = 0;
int input[MAX_CLIENTS];
int time_sum = 0;
int local_time;

void itoa(int val, char * buf ,int base){


int i = 0;
for(; val ; i++, val /= base)
buf[i] = "0123456789abcdef"[val % base];
int buflen = i;
buf[i] = 0;
for(i = 0 ; i < buflen/2 ; i++){
char temp = buf[i];
buf[i] = buf[buflen - i - 1];
buf[buflen - i - 1] = temp;
}
}

void verify(int num, char * quitMessage){


if(num < 0){
perror(quitMessage);
exit(EXIT_FAILURE);
}
}

void * sock_thread(void * sock_fd){


int client_fd = *(int *)(sock_fd);
free(sock_fd);
char buffer[MAX_BUFF], message[MAX_MESSAGE];
int len, berk_avg;
verify(len = read(client_fd, buffer, MAX_BUFF), "READ ERROR");
buffer[len] = 0;
input[num_slaves++] = atoi(buffer);
printf("Time Sent By Client %d : %s\n", num_slaves, buffer);

if(num_slaves == MAX_CLIENTS){
for(int i = 0 ; i < MAX_CLIENTS ; i++){
int time_diff = local_time - input[i];
time_sum += time_diff;
}
printf("Berkeley Sum : %d\n", time_sum);
berk_avg = time_sum / (num_slaves + 1);
printf("Avg Sum : %d\n", berk_avg);
local_time = local_time + berk_avg;
flag = 1;
}
while(flag != 1);

itoa(local_time, message, 10);


write(client_fd, message, strlen(message));
printf("Update Time Sent to Client!!\n");
close(client_fd);
pthread_exit(NULL);
}

int main(){
local_time = rand() % 24 + 1;
int server_soc, client_soc, addr_len;
SA_IN server_addr, client_addr;
verify(server_soc = socket(AF_INET, SOCK_STREAM, 0), "FAILED
TO CREATE SOCKET");

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(SERVERPORT);

verify(bind(server_soc, (SA *) &server_addr,


sizeof(server_addr)), "BIND ERROR");

verify(listen(server_soc, MAX_CLIENTS), "LISTEN FAILED");

printf("Local Server Time: %d\n", local_time);


while(1){
printf("Waiting for connection...\n");
addr_len = sizeof(SA_IN);
verify(client_soc = accept(server_soc, (SA *)&client_addr,
(socklen_t *)&addr_len), "ACCEPT FAILED");
printf("Client Connected!!\n");
pthread_t tid;
int * sock_fd = malloc(sizeof(int));
*sock_fd = client_soc;
if(pthread_create(&tid, NULL, sock_thread, sock_fd) != 0){
printf("Failed to create thread\n");
}
}

return 0;
}
====================Client Side====================

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>

#define SERVERPORT 8080


#define MAX_BUFF 1024
#define MAX_MESSAGE 30

typedef struct sockaddr SA;


typedef struct sockaddr_in SA_IN;

void itoa(int val, char * buf ,int base){


int i = 0;
for(; val ; i++, val /= base)
buf[i] = "0123456789abcdef"[val % base];
int buflen = i;
buf[i] = 0;
for(i = 0 ; i < buflen/2 ; i++){
char temp = buf[i];
buf[i] = buf[buflen - i - 1];
buf[buflen - i - 1] = temp;
}
}

void verify(int num, char * quitMessage){


if(num < 0){
perror(quitMessage);
exit(EXIT_FAILURE);
}
}

int main(){
int server_soc, readLen;
SA_IN server_addr;
char buff[MAX_BUFF], message[MAX_MESSAGE];
srand(time(0));
int local_time = rand()%24 + 1;
printf("Local Time: %d\n", local_time);

itoa(local_time, message, 10);

verify(server_soc = socket(AF_INET, SOCK_STREAM, 0), "SOCKET


CREATION FAILED");

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVERPORT);

if(inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <=


0){
printf("Ivalid Address\n");
return -1;
}

verify(connect(server_soc, (SA *) &server_addr,


sizeof(server_addr)), "CONNECTION FAILED");

write(server_soc, message, strlen(message));


printf("Local Time Sent\n");

while(1){
if(readLen = read(server_soc, buff, sizeof(buff)) > 0){
int new_time = atoi(buff);
printf("Old Local Time: %d\n", local_time);
printf("New Time: %d\n", new_time);
printf("Time Difference: %d\n", new_time -
local_time);
return 0;
}
}
return 0;
}

Output:

Server output

Client 1
Client 2

Client 3

You might also like