DOC PREVIEW
U of I CS 241 - System Programming UDP Communication

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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/2006ContentsRequest ReplyTimed messagesAcknowledged messagesDifferences between TCP and UDPAdministrativeRead R&R 20.3-20.6Read R&R 20.8MP5 is on – deadline May 1Quiz 10 – April 21Homework 2 – posted April 26 – deadline May 3, midnightQuiz 11 – April 28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 BehaviorReplies to Client with reply including senderinfo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 RetriesIf either the request or reply message is lost, or if the server crashes, client can hang forever.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;}RetriesClient cannot distinguish between server crash and lost message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,


View Full Document

U of I CS 241 - System Programming UDP Communication

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download System Programming UDP Communication
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view System Programming UDP Communication and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view System Programming UDP Communication 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?