/* UDP Example code by Kien Pham (Heavily documented to help others understand.) This code is now in Public Domain. Look ma, I did this all by myself. */ #include // Include these for socket(), bind(), etc. #include #include // Include this for getprotobyname() #include // Include this for memset() #include // Include this for htonl(), htons(), etc. #include #include // Include this for errno. #include // Include this for wait() #define PORT1 2000 #define PORT2 2001 int create_UDP_socket(int port) { int file_descriptor; struct sockaddr_in host_address; file_descriptor=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Only thing different from the TCP example is the IPPROTO_??? constant. if (file_descriptor<0) { perror("socket()"); return -1; } memset((void*)&host_address, 0, sizeof(host_address)); host_address.sin_family=AF_INET; host_address.sin_addr.s_addr=INADDR_ANY; host_address.sin_port=htons(port); if (bind(file_descriptor, (struct sockaddr*)&host_address, sizeof(host_address))<0) { perror("bind()"); return -1; } return file_descriptor; } void entity(int file_descriptor, int port) { struct sockaddr_in host_address; int host_address_size; unsigned char *address_holder; char message[]="Testing, testing, 1, 2, 3."; char buffer[256]; memset((void*)&host_address, 0, sizeof(host_address)); host_address.sin_family=AF_INET; host_address.sin_port=htons(port); address_holder=(unsigned char*)&host_address.sin_addr.s_addr; address_holder[0]=127; address_holder[1]=0; address_holder[2]=0; address_holder[3]=1; if (sendto(file_descriptor, message, sizeof(message), 0, (struct sockaddr*)&host_address, sizeof(host_address))<0) // Must use sendto() unless other care is taken. Must explicitly tell where to send each packet of data. Beyond our scope for now. (Hint: Use connect()) { perror("sendto()"); return; } memset((void*)&host_address, 0, sizeof(host_address)); host_address.sin_family=AF_INET; host_address_size=sizeof(host_address); if (recvfrom(file_descriptor, buffer, 255, 0, (struct sockaddr*)&host_address, &host_address_size)<0) // Read and put where we got the data from in host_address. { printf("%d", errno); perror("recvfrom()"); return; } printf("Port %d obtained message '%s' from host %d.%d.%d.%d.\n", port, buffer, address_holder[0], address_holder[1], address_holder[2], address_holder[3]); } int main(int number_of_arguments, char *arguments[]) { int process_id; int sock1, sock2, status; printf("\nUDP Networking Example\n"); printf("Written by Kien Pham\n"); printf("For the Networking mini-Tutorial (http://www.tripod.com/~Xengren)\n"); sock1=create_UDP_socket(PORT1); sock2=create_UDP_socket(PORT2); if ((sock1<0)||(sock2<0)) { perror("create_UDP_socket()"); return 1; } process_id=fork(); if (process_id<0) { perror("fork()"); return 1; } if (process_id!=0) { entity(sock1, PORT2); wait(&status); // This is where all the IRC ppl. helped me. So much wasted time for something so simple. Ugh. It just makes sure the other process is finished with its read before exitting and destroying its resources. if (WIFEXITED(status)==0) { printf("Child did not exit normally.\n"); return 1; } close(sock1); } else { entity(sock2, PORT1); close(sock2); } }