#include #include #include #include #include #include #include #include #define MYPORT 4950 // the port users will be connecting to #define MYPORT2 4951 #define MAXBUFLEN 100 int main() { int sockfd, sockfd_forward,i,j; struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address information int addr_len, numbytes; char buf[MAXBUFLEN]; char address[MAXBUFLEN]; char msg[MAXBUFLEN]; struct sockaddr_in their_addr_forward; // destination's address information struct hostent *h; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } if ((sockfd_forward = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(MYPORT); // short, network byte order my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } addr_len = sizeof(struct sockaddr); if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr)); printf ("packet is %d bytes long\n",numbytes); buf[numbytes] = '\0'; for(i=0;buf[i]!='+';i++) { address[i]=buf[i]; } address[i]='\0'; j=i+1; i=0; for(;jh_addr); memset(&(their_addr_forward.sin_zero), '\0', 8); if ((numbytes=sendto(sockfd_forward, msg, strlen(msg), 0, (struct sockaddr *)&their_addr_forward, sizeof(struct sockaddr))) == -1) { perror("sendto"); exit(1); } printf("Sent message to Host name : %s\n", h->h_name); printf("IP Address : %s\n", inet_ntoa(*((struct in_addr *)h->h_addr))); //close(sockfd); close(sockfd_forward); return 0; }