/* UDP torture server - receives packets from client */ #include #include #include #include #include #include #include #include #include #define SETLOCALINFO bind #define SETPEERINFO connect #define LISTENPORT 40000 #define BUFSIZE 128 int main(int argc, char** peer) { int sockfd, i, x, y,result, mlen; unsigned int plen; unsigned char buffer[BUFSIZE]; struct sockaddr_in peeraddr; // used by sendto struct sockaddr_in myaddr; // used by recvfrom sockfd = socket(AF_INET,SOCK_DGRAM,0); myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY); // accept from any source ip myaddr.sin_port = htons(LISTENPORT); plen = sizeof(peeraddr); mlen = sizeof(myaddr); bind(sockfd,(struct sockaddr *)&myaddr,mlen); // bind still needed. // Can't receive packets while other side is ready: printf("press a key to begin: "); getchar(); // why not needed for TCP? because of the 3-way handshake for(i=0;i<100000000;i++) { result = recvfrom(sockfd,buffer,BUFSIZE,0,(struct sockaddr*)&peeraddr,&plen); // extract and print sequence number inside first 4 bytes of buffer: x = *((int*)buffer); printf("packet %d received\n",x); // intensional delay to throttle server for(x=0;x<10000000;x++) { x++; x--; } } // for i /* how do I know if other side closed connection? YOU DON'T - THERE IS NO "CONNECTION"! */ close(sockfd); // only have local meaning - no packets exchanged. exit(0); }