#include #include #include #include #include #include #include #include #include #define BUFLEN sizeof(struct timeval) #define PORT 9876 void diep(char * s) { perror(s); exit(1); } int main(void) { struct sockaddr_in si_me, si_other; int s, slen = sizeof(si_other); struct timeval buf; if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) diep("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, (struct sockaddr *) &si_me, sizeof(si_me)) == -1) diep("bind"); for (;;) { if (recvfrom(s, &buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1) diep("recvfrom()"); if (sendto(s, &buf, BUFLEN, 0, (struct sockaddr *) &si_other, slen) == -1) diep("sendto()"); } close(s); return 0; }