Professional Documents
Culture Documents
21bai1660 Bcse308p Assignment5 Hrishikeshgk
21bai1660 Bcse308p Assignment5 Hrishikeshgk
21bai1660 Bcse308p Assignment5 Hrishikeshgk
AIM: To display the Calendar of a certain month in a certain year and display the current
date and time using socket programming in C.
#define BACKLOG 10
int n_client = 0;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(port);
listen(sockfd, BACKLOG);
printf("[+]Listening for the client\n");
int i = 1;
while(i){
int client_socket = accept(sockfd, NULL, NULL);
n_client++;
time_t currentTime;
time(¤tTime);
printf("Client %d requested for time at %s", n_client, ctime(¤tTime));
send(client_socket, ctime(¤tTime), 30, 0);
}
return 0
}
Client
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>
int todayOf( int y, int m, int d) /* The number of days since the beginning of the year */
{
static int DayOfMonth[] =
{ -1/*dummy*/,0,31,59,90,120,151,181,212,243,273,304,334};
lastYear = y - 1;
weekOfTopDay = days(y, m, 1) % 7;
if(isLeapYear(y))
DayOfMonth[2] = 29;
printf("\n %s %d\n%s\n", NameOfMonth[m], y, Week);
for(i=0;i<weekOfTopDay;i++)
printf(" ");
for(i=weekOfTopDay,day=1;day <= DayOfMonth[m];i++,day++){
printf("%2d ",day);
if(i % 7 == 6)
printf("\n");
}
printf("\n");
}
return 0;
}
To Run,
1) gcc server.c -o server (run on server.c file)
2) ./server
3) gcc client.c -o client (run on client.c file)
4) ./client
OUTPUT
Server side
Client side
Conclusion
We were to able to display the output for calendar, date on the terminal using socket
programming in C language after having connected the server-client sockets with each other
and establishing a connection between them.
=====THE END=====