#include #include #include #include #include #include #include #include #include #include /* void printsin(struct sockaddr_in *s, char *str1, char *str2) { printf("%s\n", str1); printf("%s: ", str2); -- port: sin->sin_port (host integer type) -- IP: sin->sin_addr (IP in dotted-decimal notation) printf("\n"); } */ int main(int argc, char *argv[]) { int socket_fd, cc, fsize; struct sockaddr_in s_in, from; struct { char head; u_long body; char tail;} msg; socket_fd = socket (AF_INET, SOCK_DGRAM, 0); bzero((char *) &s_in, sizeof(s_in)); /* They say you must do this */ s_in.sin_family = (short)AF_INET; s_in.sin_addr.s_addr = htonl(INADDR_ANY); /* WILDCARD */ s_in.sin_port = htons((u_short)0x3333); //printsin( &s_in, "RECV_UDP", "Local socket is:"); fflush(stdout); bind(socket_fd, (struct sockaddr *)&s_in, sizeof(s_in)); for(;;) { fsize = sizeof(from); cc = recvfrom(socket_fd,&msg,sizeof(msg),0,(struct sockaddr *)&from,&fsize); //printsin( &from, "recv_udp: ", "Packet from:"); printf("Got data ::%c%ld%c\n",msg.head,(long) ntohl(msg.body),msg.tail); fflush(stdout); } return 0; }