CS241 System Programming UDP Communication (IV)ContentsAdministrativeSimple –Request ProtocolsServer_udp.cclient_udp.cRequest–Reply ProtocolsClient/Server BehaviorServer request-reply protocolClient sends process ID and reads replyRequest-reply –error free deliveryTimeouts and RetriesState Diagram of Client for Request-ReplySimple TimeoutRetriesState Diagram of Client for Request-Reply with Time-OutsRequest-Reply with Timeouts and RetriesRequest-Reply-Acknowledge ProtocolsState Diagram of Client for Request-Reply-AcknowledgeServer BehaviorComparison UDP and TCPSummaryCS241 System Programming UDP Communication (IV)Klara NahrstedtLecture 374/21/2006Contentsz Request Replyz Timed messagesz Acknowledged messagesz Differences between TCP and UDPAdministrativez Read R&R 20.3-20.6z Read R&R 20.8z MP5 is on – deadline May 1z Quiz 10 – April 21z Homework 2 – posted April 26 – deadline May 3, midnightz Quiz 11 – April 28z Quiz 12 – May 3 – OPTIONAL!!!!! ONLY TEN BEST RESULTS FROM QUIZZES WILL COUNT AS SPECIFIED AT THE BEGINNING OF THE SEMESTERSimple –Request Protocolsu_openudp(p)u_openudp(p)u_sendtohost()closeu_recvfrom()UICI UDP ServerUICI UDP Clientcreate UDP endpointbound to port pCreate unbound UDP endpointWait for requestsend requestprocess requestresume waitingclose endpointrequest messageServer_udp.cint main(int argc, char *argv[]) {…port = (u_port_t) atoi(argv[1]); /* create communication endpoint */if ((requestfd = u_openudp(port)) == -1) {perror("Failed to create UDP endpoint");return 1;}for ( ; ; ) { /* process client requests */bytesread = u_recvfrom(requestfd, buf, BUFSIZE, &senderinfo);if (bytesread < 0) {perror("Failed to receive request");continue;}u_gethostinfo(&senderinfo, hostinfo, BUFSIZE);if ((r_write(STDOUT_FILENO, hostinfo, strlen(hostinfo)) == -1) ||(r_write(STDOUT_FILENO, buf, bytesread) == -1)) {perror("Failed to echo reply to standard output");}}}client_udp.cint main(int argc, char *argv[]) {…serverport = (u_port_t) atoi(argv[2]);if ((requestfd = u_openudp(0)) == -1) {/*create unbnd UDP endpoint*/perror("Failed to create UDP endpoint");return 1;}sprintf(request, "[%ld]\n", (long)getpid()); /* create a request */rlen = strlen(request);/* simple-request protocol sends a request to (server, serverport) */byteswritten = u_sendtohost(requestfd, request, rlen, argv[1], serverport);if (byteswritten == -1)perror("Failed to send");if (r_close(requestfd) == -1 || byteswritten == -1)return 1;return 0;}Request–Reply Protocolsu_openudp(p)u_openudp(p)u_sendtohost()closeu_recvfrom()UICI UDP ServerUICI UDP Clientcreate UDP endpointbound to port pCreate unbound UDP endpointWait for request send requestprocess requestresume waitingclose endpointrequest messageu_sendto()send responsereply messageu_recvfrom()wait for responseClient/Server Behaviorz Replies to Client with reply including senderinfoz Client checks reply to make sure it comes from server and it should check to see that the message contains senderinfo from clientServer request-reply protocolint main(int argc, char *argv[]) {…for ( ; ; ) { /* process client requests and send replies */bytesread = u_recvfrom(requestfd, buf, BUFSIZE, &senderinfo);if (bytesread == -1) {perror("Failed to receive client request");continue;}u_gethostinfo(&senderinfo, hostinfo, BUFSIZE);if ((r_write(STDOUT_FILENO, hostinfo, strlen(hostinfo)) == -1) ||(r_write(STDOUT_FILENO, buf, bytesread) == -1)) {perror("Failed to echo client request to standard output");}if (u_sendto(requestfd, buf, bytesread, &senderinfo) == -1) {perror("Failed to send the reply to the client");}} }Client sends process ID and reads replyint main(int argc, char *argv[]) {…bytesread = request_reply(requestfd, request, strlen(request)+1,argv[1], serverport, reply, BUFSIZE);if (bytesread == -1) perror("Failed to do request_reply");else {byteswritten = r_write(STDOUT_FILENO, reply, bytesread);if (byteswritten == -1)perror("Failed to echo server reply");}if ((r_close(requestfd)==-1)||(bytesread==-1)||(byteswritten==-1))return 1;return 0;}Request-reply –error free delivery#include <sys/types.h>#include "uiciudp.h"int request_reply(int requestfd, void* request, int reqlen,char* server, int serverport, void *reply, int replen) {ssize_t nbytes;u_buf_t senderinfo;/* send the request */nbytes = u_sendtohost(requestfd, request, reqlen, server, serverport);if (nbytes == -1)return (int)nbytes;/* wait for a response, restart if from wrong server */while ((nbytes = u_recvfrom(requestfd,reply,replen,&senderinfo))>=0) if (u_comparehost(&senderinfo, server, serverport)) /*sender match*/break;return (int)nbytes;}Timeouts and Retriesz If either the request or reply message is lost, or if the server crashes, client can hang forever.z Use select and software timer or use timeout provision on socketState Diagram of Client for Request-Replyreturn replyreturn -1(EINVAL)send request wait for replyreturn -1(ETIME)return -1bad hostsend errorreceive errorsuccess successtime outwrong replySimple Timeoutint request_reply_timeout(int requestfd, void* request, int reqlen,char* server, int serverport, void *reply, int replen,double timeout) {ssize_t nbytes;u_buf_t senderinfo;/* send the request */nbytes = u_sendtohost(requestfd, request, reqlen, server, serverport);if (nbytes == -1)return -1;/* wait timeout seconds for a response, restart if from wrong server */while ((nbytes = u_recvfromtimed(requestfd, reply, replen,&senderinfo, timeout)) >= 0 &&(u_comparehost(&senderinfo, server, serverport) == 0)) ;return (int)nbytes;}Retriesz Client cannot distinguish between server crash and lost messagez Client resets timeout each time it encounters an incorrect responder – allows denial-of-service attack where offenders continually send spurious packets to ports on attacked machine.State Diagram of Client for Request-Reply with Time-Outsreturn replyreturn -1(EINVAL)send request wait for replyreturn -1(ETIME)return -1bad hostsend errorreceive errorsuccess successretries exceededwrong replytimeoutRequest-Reply with Timeouts and Retriesint request_reply_timeout_retry(int requestfd, void* request, int reqlen,char* server, int serverport, void *reply, int replen,double timeout, int maxretries) { … retries = 0; while (retries < maxretries){/*send process ID to (server, serverport)*/nbytes = u_sendtohost(requestfd, request, reqlen, server,serverport);if (nbytes == -1)return -1; /*
View Full Document