/* NAME: udpServer0 SYNOPSIS: udpServer0 DESCRIPTION: The program creates a datagram socket in the inet domain, binds it to port 10111 and receives any message arrived to the socket and prints it out */ #include "def" main() { int sd; struct sockaddr_in server; struct sockaddr_in cliaddr; char buf[512]; int rc; int len; server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(10111); sd = socket (AF_INET,SOCK_DGRAM,0); if ( bind( sd, (SA *) &server, sizeof(server) ) < 0 ){ printf("error binding\n"); exit(-1) ; } for(;;){ /* use read, recv or recvfrom rc=read (sd, buf, sizeof(buf)); rc=recv (sd, buf, sizeof(buf), 0); */ len = sizeof(struct sockaddr_in); rc = recvfrom (sd, buf, sizeof(buf), 0, (SA *) &cliaddr, &len); buf[rc]= (char) NULL; printf("Received: %s\n", buf); } }