0% found this document useful (0 votes)
7 views3 pages

Program Server

The document contains C code for a UDP server and client. The server listens for a 'gettime' message and responds with the current time every second, while the client sends a message to the server and continuously receives time updates. Both components utilize the socket API for communication over the local network.

Uploaded by

foreverbo089
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)
7 views3 pages

Program Server

The document contains C code for a UDP server and client. The server listens for a 'gettime' message and responds with the current time every second, while the client sends a message to the server and continuously receives time updates. Both components utilize the socket API for communication over the local network.

Uploaded by

foreverbo089
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
You are on page 1/ 3

PROGRAM

SERVER

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
void main()
{
​ int sfd,len;
​ char msg[50];
​ struct sockaddr_in servaddr;
​ struct tm *info;
​ time_t rawtime;
​ sfd=socket(AF_INET,SOCK_DGRAM,0);
​ if(sfd<0)
​ printf("Socket error\n");
​ else
​ printf("Socket Success\n");

​ servaddr.sin_family=AF_INET;
​ servaddr.sin_port=htons(8001);
​ servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
​ len=sizeof(servaddr);

​ if(bind(sfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
​ {
​ printf("Bind error\n");
​ exit(0);
​ }
printf("Bind success\n");

bzero(msg,50);
recvfrom(sfd,msg,sizeof(msg),0,(struct sockaddr *)&servaddr,&len);
if(strncmp("gettime",msg,7)==0)
{
while(1)
{
time(&rawtime);
info=localtime(&rawtime);
strcpy(msg,asctime(info));
sendto(sfd,msg,sizeof(msg),0,(struct sockaddr *)&servaddr,len);
sleep(1);

}
}

CLIENT

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
void main()
{
​ int sfd,len;
​ char msg[50];
​ struct sockaddr_in servaddr;
​ struct tm *info;
​ time_t rawtime;
​ sfd=socket(AF_INET,SOCK_DGRAM,0);
​ if(sfd<0)
​ printf("Socket error\n");
​ else
​ printf("Socket Success\n");


​ servaddr.sin_family=AF_INET;
​ servaddr.sin_port=htons(8001);
​ servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
​ len=sizeof(servaddr);

​ bzero(msg,50);
​ printf("Msg to Server:");
​ fgets(msg,sizeof(msg),stdin);
sendto(sfd,msg,sizeof(msg),0,(struct sockaddr *)&servaddr,len);
while(1)
{
recvfrom(sfd,msg,sizeof(msg),0,(struct sockaddr *)&servaddr,&len);
​ printf("%s",msg);
​ }
}

OUTPUT

SERVER

CLIENT

You might also like