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

Assignment 1

The document presents a concurrent day-time client-server application implemented in C using UDP sockets. The server listens for client requests and responds with the current date and time, while the client sends a request to the server. Both server and client code are provided, demonstrating the basic functionality of sending and receiving messages over a network.

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)
4 views

Assignment 1

The document presents a concurrent day-time client-server application implemented in C using UDP sockets. The server listens for client requests and responds with the current date and time, while the client sends a request to the server. Both server and client code are provided, demonstrating the basic functionality of sending and receiving messages over a network.

Uploaded by

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

Program - 1

Aim: Implement concurrent day-time client-server application.

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 <time.h>

#define PORT 8080


#define MAXLINE 1024

int main() {
char buffer[MAXLINE];
struct sockaddr_in serverAddress, clientAddress;
int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);

if(socket_fd < 0){


printf("Socket creation failed");
exit(EXIT_FAILURE);
}

memset(&serverAddress, 0, sizeof(serverAddress));
memset(&clientAddress, 0, sizeof(clientAddress));

serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT);

int bind_code = bind(socket_fd, (const struct sockaddr


*)&serverAddress, sizeof(serverAddress));

if(bind_code < 0){


perror("bind failed");
exit(EXIT_FAILURE);
}

printf("Waiting for Client...\n");


unsigned int sizeCliAddr = sizeof(clientAddress);
int len = recvfrom(socket_fd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *) &clientAddress, &sizeCliAddr);
buffer[len] = '\0';
printf("Client Message Received: %s\n", buffer);

time_t curtime;
time(&curtime);

const char * message = ctime(&curtime);


sendto(socket_fd, message, strlen(message), MSG_CONFIRM,
(const struct sockaddr *) &clientAddress, sizeCliAddr);
printf("Current Date and Time Sent.\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 <time.h>

#define PORT 8080


#define MAXLINE 1024

int main() {

char buffer[MAXLINE];
struct sockaddr_in serverAddress, clientAddress;
int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
char message[] = "GET DATE AND TIME";

if(socket_fd < 0){


printf("Socket creation failed");
exit(EXIT_FAILURE);
}

memset(&serverAddress, 0, sizeof(serverAddress));

serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT);

int len, temp;


printf("Sending Message: %s\n", message);

sendto(socket_fd, message, strlen(message), MSG_CONFIRM,


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

printf("Message Sent\n");

len = recvfrom(socket_fd, (char *) buffer, MAXLINE,


MSG_WAITALL, (struct sockaddr *) &serverAddress, &temp);
buffer[len] = 0;
printf("Server Returned : %s\n", buffer);

close(socket_fd);
return 0;
}
Output:

You might also like