#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<sys/time.h>
#include<arpa/inet.h>
int main()
{
int server_sockfd,client_sockfd;
int server_len,client_len;
char msg1[100],msg2[100];
time_t t;
struct sockaddr_in server_address,client_address;
server_sockfd=socket(AF_INET,SOCK_STREAM,0);
server_address.sin_family=AF_INET;
server_address.sin_port=4006;
server_address.sin_addr.s_addr=inet_addr("127.0.0.1");
server_len=sizeof(server_address);
bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
listen(server_sockfd,5);
client_len=sizeof(client_address);
client_sockfd=accept(server_sockfd,(struct sockaddr
*)&client_address,&client_len);
read(client_sockfd,msg2,sizeof(msg2));
puts(msg2);
t=time(NULL);
sprintf(msg1,"%24s",ctime(&t));
write(client_sockfd,msg1,sizeof(msg1));
close(client_sockfd);
}
Client side
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<arpa/inet.h>
#include<unistd.h>
int main()
{
int sockfd,len,result;
struct sockaddr_in address;
char msg1[100],msg2[100];
sockfd=socket(AF_INET,SOCK_STREAM,0);
address.sin_family=AF_INET;
address.sin_port=4006;
address.sin_addr.s_addr=inet_addr("127.0.0.1");
len=sizeof(address);
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result == -1)
{
perror("oops : client1");
exit(1);
}
printf("Enter::");
gets(msg2);
write(sockfd,msg2,sizeof(msg2));
read(sockfd,msg1,sizeof(msg1));
printf("\n Time of server is::");
puts(msg1);
return(0);
}
0 Comments