21bai1660 Bcse308p Assignment5 Hrishikeshgk

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

BCSE308P – COMPUTER NETWORKS LAB – ASSIGNMENT 5

FACULTY: Prof. SAHAYA BENI PRATHIBA B MA’AM

NAME: HRISHIKESH G KULKARNI Date: 18/01/23

REG.NO.: 21BAI1660 – VIT CHENNAI-SCOPE– B. Tech CSE w/ Spec. In AI/ML

To Display the calendar, date and time using Socket Programming in C

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.

Steps for the connection


Server
1. using create(), Create socket.
2. using bind(), Bind the socket to server address.
3. using listen(), put the server socket in a passive mode, where it waits for the client to
approach the server to make a connection
4. using accept(), At this point, connection is established between client and server, and
they are ready to communicate
5. Create function for showing client request and display date/time and close socket.
Client
1. Create socket.
2. connect newly created client socket to server.
3. Function for calendar display, close socket.

Implementation of Calculator Application


CODES
Server
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>

#define BACKLOG 10

int main(int argc, char **argv){


if(argc != 2){
printf("Usage: %s <port>\n", argv[0]);
exit(0);
}
int port = atoi(argv[1]);
printf("Port: %d\n", port);

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);

bind(sockfd, (struct sockaddr*)&serverAddress, sizeof(serverAddress));


printf("[+]Bind\n");

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(&currentTime);
printf("Client %d requested for time at %s", n_client, ctime(&currentTime));
send(client_socket, ctime(&currentTime), 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 isLeapYear( int year ); /* True if leap year */


int leapYears( int year ); /* The number of leap year */
int todayOf( int y, int m, int d); /* The number of days since the beginning of the year */
long days( int y, int m, int d); /* Total number of days */
void calendar(int y, int m);

int isLeapYear( int y ) /* True if leap year */


{
return(y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0));
}

int leapYears( int y ) /* The number of leap year */


{
return y/4 - y/100 + y/400;
}

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};

return DayOfMonth[m] + d + ((m>2 && isLeapYear(y))? 1 : 0);


}

long days( int y, int m, int d) /* Total number of days */


{
int lastYear;

lastYear = y - 1;

return 365L * lastYear + leapYears(lastYear) + todayOf(y,m,d);


}
void calendar(int y, int m) /* display calendar at m y */
{
const char *NameOfMonth[] = { NULL/*dummp*/,
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
char Week[] = "Su Mo Tu We Th Fr Sa";
int DayOfMonth[] =
{ -1/*dummy*/,31,28,31,30,31,30,31,31,30,31,30,31 };
int weekOfTopDay;
int i,day;

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");
}

int main(int argc, char **argv){


int year;
int month;
printf("Enter the month and year: ");
scanf("%d %d", &month, &year);
calendar(year, month);
if(argc != 2){
printf("Usage: %s <port>\n", argv[0]);
exit(0);
}

int port = atoi(argv[1]);


printf("Port: %d\n", port);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
char response[30];
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(port);
connect(sockfd, (struct sockaddr*)&serverAddress, sizeof(serverAddress));
printf("[+]Connected to the server\n");
recv(sockfd, response, 29, 0);
printf("Time from server: %s", response);

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=====

You might also like