How to Find Day Time Server to Client | Source code in C Programming

To print the day and time in the client side using the socket functions.
Server Client Day Time Algorithm
Server side programming Algorithm
STEP 1: Start the program.
STEP 2: Declare the variables and structure for the socket..How to Find Day Time Server to Client  Source code in C Programming
STEP 3: The socket is binded at the specified port.
STEP 4: Using the object the port and address are declared.
STEP 5: The listen and accept functions are executed.
STEP 6: If the binding is successful it waits for client request.
STEP 7: Execute the client program.
CLIENT DAY TIME ALGORITHM
STEP 1: Start the program.
STEP 2: Declare the variables and structure.
STEP 3: Socket us created and connect function is executed.
STEP 4: If the connection is successful then server sends the message.
STEP 5: The date and time is printed at the client side.
STEP 6: Stop the program.
SERVER PROGRAM SOURCE CODE IN C
#include<netinet/in.h>
#include<sys/socket.h>
main( )
{
struct sockaddr_in sa;
struct sockaddr_in cli;
int sockfd,coontfd;
int len,ch;
char str[100];
time_t tick;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(socket<0)
{
printf(“error in socket\n”);
exit(0);
}
else
printf(“Socket Opened”);
bzero(7sa,sizeof(sa));
sa.sin_port=htons(5600);
sa.sin_addr.s_addr=htonl(0);
if(bind(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0)
{
printf(“Error in binding\n”);
}
else
printf(“Binded Successfully”);
listen(sockfd,50)
for(;;)
{
len=sizeof(ch);
conntfd=accept(sockfd,(struct sockaddr*)&cli,&len);
printf(“Accepted”);
tick=ctime(NULL);
snprintf(str,sizeof(str),”%s”,ctime(&tick));
write(conntfd,str,100);
}
}
CLIENT PROGRAM Source code in C
#include<netinet/in.h>
#include<sys/socket.h>
main()
{
struct sockaddr_in sa,cli;
int n,sockfd;
int len;
char buff[100];
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf(“Error in Socket”);
exit(0);
}
else
printf(“Socket is Opened”);
bzero(&sa,sizeof(sa));
sa.sin_family=AF_INET;
sa.sin_port=htons(5600);
if(connect(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0)
{
printf(“Error in connection failed”);
exit(0);
}
else
printf(“connected successfully”):
if(n=read(sockfd,buff,sizeof(buff))<0)
{
printf(“Error in Reading”);
exit(0);
}
else
{
printf(“Message Read %s”,buff);
buff[n]=’\0’;
printf(“%s”,buff);
}
}
Output CS1305 Network Lab
SERVER SIDE OUTPUT
Socket Opened
Binded successfully
Accepted
CLIENT SIDE OUTPUT WITH SERVER TIME
Socket is Opened
Connected successfully
Message Read Mon Jan 22 15:09:19 2007
RESULT Thus the program for getting the day and time of the server using TCP has been created and verified. CS1305 Network Lab - Free C programming Source code in c client server Day time.