Ads Here

Monday, March 11, 2019

UDP Iterative Server


UDP Iterative Server

SERVER: #include<netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include<stdio.h> #include <arpa/inet.h> #include <string.h> #include<fcntl.h> main() { int sfd,l; char buf[1024]="",buf1[1024]=""; struct sockaddr_in server,client; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(1301); inet_aton("localhost",&server.sin_addr); printf("bind=%d\n" ,bind(sfd,(struct sockaddr *)&server,sizeof(server))); l=sizeof(client); for(;;) { recvfrom(sfd,buf,1024,0,(struct sockaddr *)&client,&l); printf("MESSAGE FROM CLIENT:%s\n",buf); printf("Enter the message:"); scanf("%s",buf1); sendto(sfd,buf1,strlen(buf1),0,(struct sockaddr *)&client,l); } } CLIENT: #include<netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include<stdio.h> #include <arpa/inet.h> #include <string.h> #include<fcntl.h> main() { int sfd,l; char buf[1024]="",buf1[1024]=""; struct sockaddr_in ser; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&ser,sizeof(ser)); ser.sin_family=AF_INET; ser.sin_port=htons(1301); inet_aton("localhost",&ser.sin_addr); printf("Enter the message:"); scanf("%s",buf); l=sizeof(ser); sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&ser,l); recvfrom(sfd,buf1,1024,0,(struct sockaddr *)&ser,&l); printf("message from server:%s\n",buf1); close(sfd); }

No comments:

Post a Comment