Choose Category
tcpechoserver.c #include<stdio.h> #include<sys/types.h> #include<string.h> #include<stdlib.h> #include<sys/socket.h> #include<arpa/inet.h> #include<unistd.h> #define SA struct sockaddr #define LISTENQ 5 int main(int argc,char **argv) { int fd,sockfd,listenfd,connfd; pid_t childpid; socklen_t client; struct sockaddr_in Servaddr,cliaddr; 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(atoi(argv[1])); bind(listenfd,(SA*)&Servaddr,sizeof(Servaddr)); listen(listenfd,LISTENQ); client=sizeof(cliaddr); connfd=accept(listenfd,(SA*)&cliaddr,&client); char buffer[100],buf[100]; printf("\n The received from client content"); read(connfd,buffer,100); printf("%s",buffer); strcpy(buffer,buf); write(connfd,buf,100); printf("\n The content is echod"); }
tcpechoclient.c #include<stdio.h> #include<sys/types.h> #include<string.h> #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<string.h> #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<string.h> #include<stdlib.h> #include<arpa/inet.h> #include<unistd.h> #define SA struct sockaddr int main(int argc,char **argv) { int sockfd; char fname[25]; int len; struct sockaddr_in servaddr,cliaddr; sockfd=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(atoi(argv[1])); inet_pton(AF_INET,argv[1],&servaddr.sin_addr); connect(sockfd,(SA*)&servaddr,sizeof(servaddr)); char buffer[100],buf[100]; printf("\n Enter connect 2 echo"); scanf("%s",buffer); write(sockfd,buffer,100); printf("\n The content is sent \n"); read(sockfd,buf,100); printf("\n The echod content %s",buffer); }