/* udp4-r2.c: bind to wildcard IP address */ #include #include #include #include #include #include #include #include #define MAXSIZE 100 /* ----------------------------------------------------------------- paddr: print the IP address in a standard decimal dotted format ----------------------------------------------------------------- */ void paddr(unsigned char *a) { printf("%d.%d.%d.%d", a[0], a[1], a[2], a[3]); } main(int argc, char *argv[]) { int s; /* s = socket */ struct sockaddr_in in_addr; /* Structure used for bind() */ struct sockaddr_in sock_addr; /* Output structure from getsockname */ struct sockaddr_in src_addr; /* Used to receive (addr,port) of sender */ int src_addr_len; /* Length of src_addr */ int len; /* Length of result from getsockname */ char line[MAXSIZE]; if (argc == 1) { printf("Usage: %s port\n", argv[0]); exit(1); } /* --- Create a socket --- */ s = socket(PF_INET, SOCK_DGRAM, 0); /* --- Set up socket end-point info for binding --- */ memset((char *)&in_addr, 0, sizeof(in_addr)); in_addr.sin_family = AF_INET; /* Protocol family */ in_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* Wildcard IP address */ in_addr.sin_port = htons(atoi(argv[1])); /* Use any UDP port */ /* --- Here goes the binding... --- */ if (bind(s, (struct sockaddr *)&in_addr, sizeof(in_addr)) == -1) { perror("bind: "); printf("- Note: this program can be run on any machine...\n"); printf(" but you have to pick an unsed port#\n"); exit(1); } else { printf("OK: bind SUCCESS\n"); } /* -------------------------------------------------------- **** Print socket "name" (which is IP-address + Port#) -------------------------------------------------------- */ len = sizeof(sock_addr); getsockname(s, (struct sockaddr *) &sock_addr, &len); printf("Socket is bind to:\n"); printf(" addr = %u\n", ntohl(sock_addr.sin_addr.s_addr) ); printf(" port = %u\n", ntohs(sock_addr.sin_port) ); while(1) { src_addr_len = sizeof(src_addr); len = recvfrom(s, line, MAXSIZE, 0 /* flags */, (struct sockaddr *) &src_addr, &src_addr_len); printf("Msg from ("); paddr( (void*) &src_addr.sin_addr); printf("/%u): `%s' (%u bytes)\n", src_addr.sin_port, line, len); } }