#include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in servaddr, cli; sockfd = socket(AF_INET, SOCK_DGRAM, 0); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); int port = atoi(argv[1]); servaddr.sin_port = htons(port); bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); socklen_t len = sizeof(cli); while(1) { int n; char *buf = malloc(100); while( (n = recvfrom(sockfd, (char *)buf, 100, MSG_WAITALL, (struct sockaddr*) &cli, &len)) > 0 ) { printf("Client message: %s\n", buf); fflush(stdout); sendto(sockfd, buf, n, MSG_CONFIRM, (const struct sockaddr *) &cli, len); } } close(sockfd); return 0; }