0% found this document useful (0 votes)
6 views2 pages

Concurrent Time Serve Using UDP

The document provides a code implementation for a concurrent time server using UDP. It includes both client and server code, where the client sends messages to the server and receives the current time in response. The server handles multiple client requests by forking a new process for each message received, sending back the current time until a 'stop' message is received.

Uploaded by

Althaf Asharaf
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Concurrent Time Serve Using UDP

The document provides a code implementation for a concurrent time server using UDP. It includes both client and server code, where the client sends messages to the server and receives the current time in response. The server handles multiple client requests by forking a new process for each message received, sending back the current time until a 'stop' message is received.

Uploaded by

Althaf Asharaf
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Concurrent Time Server Using UDP

CLIENT

#include<stdio.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
void main()
{
printf("Client1\n");
char buffer[50];
int sockfd;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=INADDR_ANY;
addr.sin_port=5000;
int s=sizeof(struct sockaddr_in);
connect(sockfd,(struct sockaddr *)&addr,sizeof(addr));
do
{
printf("Enter msg for server:");
scanf("%s",buffer);
if(strcmp(buffer,"stop")==0)
break;
sendto(sockfd,buffer,sizeof(buffer),0,(struct sockaddr*)&addr,s);
recvfrom(sockfd,buffer,sizeof(buffer),0,(struct sockaddr*)&addr,&s);
printf("Received from server:%s\n",buffer);
}while(strcmp(buffer,"stop")!=0);
close(sockfd);
}

SERVER

#include<stdio.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/types.h>
#include<time.h>
#include<string.h>
void main()
{
printf("Server Side:!\n");
char buffer[50];
int sockfd;
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=INADDR_ANY;
addr.sin_port=5000;
int s=sizeof(struct sockaddr_in);
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
printf("Connection Established..\n");
do
{
recvfrom(sockfd,buffer,sizeof(buffer),0,(struct sockaddr*)&addr,&s);
printf("Message from client:%s\n",buffer);
pid_t pid=fork();
if(pid==0)
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
sprintf(buffer,"Time is %d:%d:%d",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
sendto(sockfd,buffer,sizeof(buffer),0,(struct sockaddr *)&addr,s);
printf("Time sent to client.\n");
}
}while(strcmp(buffer,"stop")!=0);
close(sockfd);
}

OUTPUT

CLIENT

Client1
Enter msg for server:hai
Received from server:Time is 15:34:44
Enter msg for server:hello
Received from server:Time is 15:34:56

SERVER

Server Side:!
Connection Established..
Message from client:hai
Time sent to client.
Message from client:hello
Time sent to client.

You might also like