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

Daytimeclientserver Program

This document contains code for a client and server program that communicate date and time information. The client program connects to the server, which is listening on port 4567, and reads and prints the date/time string sent by the server. The server program binds to port 4567, accepts connections, gets the current time, formats it into a string, writes it to the connected client, and closes the connection. This is repeated for each new client connection.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Daytimeclientserver Program

This document contains code for a client and server program that communicate date and time information. The client program connects to the server, which is listening on port 4567, and reads and prints the date/time string sent by the server. The server program binds to port 4567, accepts connections, gets the current time, formats it into a string, writes it to the connected client, and closes the connection. This is repeated for each new client connection.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/* Cleint Program for day & time */ #include<unp.h> #include<sys/types.h> #include<sys/socket.

h> int main(int argc, char **argv) { int sockfd,n; char recvline[MAXLINE+1]; struct sockaddr_in servaddr; if(argc != 2) printf("usage: a.out <IPADDRESS>"); if((sockfd=socket(AF_INET, SOCK_STREAM,0))<0) printf("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(4567); //port number if given as 13 it will not work so use 4567 or dynamic ports if(inet_pton(AF_INET,argv[1],&(servaddr.sin_addr))<=0) printf("inet_pton error for %s\n",argv[1]); if(connect(sockfd,(struct sockaddr *) &servaddr,sizeof(servaddr))<0) printf("connect Error\n"); while((n=read(sockfd,recvline,MAXLINE))>0) { recvline[n]=0; if(fputs(recvline,stdout)==EOF) printf("fputs error\n"); } if(n<0) printf("read error\n"); exit(0); } /* server Program for Day & time #include<unp.h> #include<time.h> int main(int argc, char **argv) { int listenfd,confd; struct sockaddr_in servaddr; char buff[MAXLINE]; time_t ticks; listenfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(4567); bind(listenfd,(SA *) &servaddr,sizeof(servaddr)); listen(listenfd,LISTENQ); for( ; ; ) */

{ confd=accept(listenfd,(SA *) NULL,NULL); ticks=time(NULL); snprintf(buff,sizeof(buff),"%.24s\r\n",ctime(&ticks)); write(confd,buff,strlen(buff)); close(confd); } }

You might also like