Assignment 2
Assignment 2
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>
int num_slaves = 0;
int flag = 0;
int input[MAX_CLIENTS];
int time_sum = 0;
int local_time;
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);
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);
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>
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);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVERPORT);
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