/* * Simple datagram receiver * (c)2013 JBY */ #include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <strings.h> #include <unistd.h> #include <stdlib.h> int main(int argc,char *argv[]) { int s, r; struct sockaddr_in sin; char msg[100]; s = socket(AF_INET,SOCK_DGRAM,0); if (s==-1) { perror("socket"); exit(EXIT_FAILURE); } bzero(&sin,sizeof(sin)); sin.sin_family = PF_INET; sin.sin_port = htons(60123); sin.sin_addr.s_addr = INADDR_ANY; r = bind(s,(struct sockaddr *)&sin,sizeof(sin)); if (r==-1) { perror("bind"); close(s); exit(EXIT_FAILURE); } while (1) { r = recv(s,msg,100,0); if (r==-1) { perror("recv"); close(s); exit(EXIT_FAILURE); } msg[r] = '\0'; printf("Recu: %s\n",msg); } }