#include #include #include #include #include #include #include #include #include #define SETLOCALINFO bind #define SETPEERINFO connect #define LISTENPORT 60001 int main(int argc, char** argv) // argv = ip port intx { int sockfd, x, y, result, plen, llen, slen; struct sockaddr_in localinfo; struct sockaddr_in peerinfo; struct sockaddr_in senderinfo; sockfd = socket(AF_INET,SOCK_DGRAM,0); localinfo.sin_family = AF_INET; localinfo.sin_addr.s_addr = htonl(INADDR_ANY); localinfo.sin_port = htons(LISTENPORT); peerinfo.sin_family = AF_INET; peerinfo.sin_addr.s_addr = inet_addr(argv[1]); peerinfo.sin_port = htons(atoi(argv[2])); llen = sizeof(localinfo); plen = sizeof(peerinfo); slen = sizeof(senderinfo); SETLOCALINFO(sockfd,(struct sockaddr*)&localinfo,llen); printf("press a key to start: "); getchar(); x = atoi(argv[3]); y = htonl(x); result = 0; while (result < 4) result = sendto(sockfd,&y,4,0,(struct sockaddr*)&peerinfo,plen); result = 0; while (result < 4) result = recvfrom(sockfd,&y,4,0, (struct sockaddr*)&senderinfo,&slen); x = ntohl(y); printf("got %d from peer %s, port %d\n",x, inet_ntoa(senderinfo.sin_addr), ntohs(senderinfo.sin_port)); close(sockfd); exit(0); }