/* UDP torture client - sends info to server It's a "client" because it sends the first packet */ #include #include #include #include #include #include #include #include #include #define SETLOCALINFO bind #define SETPEERINFO connect /* Concept of client-server initialization doesn't exist with udp - There is still a server (with well-known as oppposed to ephemeral port), but no handshake is used to establish who the server is, and who the client is. In other words, anybody can call themselves servers - whether the "clients" will respect that depends not on the protocol but on how the clients will behave. recvfrom is a more generalized form of accept sendto is a more generalized form of connect In other words, each transaction is like a separate connection. In UDP, unlike TCP, "client-server" is a more general concepts - it's dictated by the nature of the application rather than the protocal. There is no acknowlegment service, defragmentation service, or congestion control. There is a "buffer" attached to the file descriptor, but this has local meaning only. */ #define LISTENPORT 40000 #define BUFSIZE 128 int main(int argc, char** peer) // peer = ip { int sockfd, r, y,result, plen, mlen; 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); peeraddr.sin_family = AF_INET; peeraddr.sin_addr.s_addr = inet_addr(peer[1]); peeraddr.sin_port = htons(LISTENPORT); 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 send packets while other side is ready: printf("press a key to begin: "); getchar(); // why not needed for TCP? because of the 3-way handshake // assign some data (ascii-range values) to buffer: // First 4 bytes of buffer will indicate a sequence number: for(r=4;r