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); }
Monday, March 11, 2019
UDP Iterative Server
Subscribe to:
Post Comments (Atom)
he bzero function can be used to erase the given memory area with zero bytes (\0). It takes two arguments, the starting address of the memory region and the number of bytes that need to be zeroed out.
ReplyDeleteThe bzero function can be used to erase the given memory area with zero bytes (\0). It takes two arguments, the starting address of the memory region and the number of bytes that need to be zeroed out.
ReplyDeleteinet_aton() converts the Internet host address localhost from the IPv4 numbers-and-dots notation into binary form (in network byte order) and stores it in the structure that ser points to. inet_aton() returns nonzero if the address is valid, zero if not.
ReplyDeleteINADDR_ANY is a constant, that contain 0 in value . this will used only when you want connect from all active ports you don't care about ip-add . so if you want connect any particular ip you should mention like as my_sockaddress.sin_addr.s_addr = inet_addr("192.168.78.2")
ReplyDelete