DOC PREVIEW
USC CSCI 551 - 09_warmup1-6up

This preview shows page 1 out of 3 pages.

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

Unformatted text preview:

CS551Warm-up Project #1Bill Chenghttp://merlot.usc.edu/cs551-f121 Computer Communications - CSCI 551 Copyright © William C. Chengtypedef struct tagReqMsg { unsigned short MsgType; unsigned int Offset; unsigned char ServerDelay; unsigned int DataLen; char *Data;} ReqMsg;int SendReq(int n_socket){ ReqMsg request; memset(&request, 0, sizeof(ReqMsg)); /* fill up the request data structure */ if (write(n_socket, &request, sizeof(ReqMsg)) == sizeof(ReqMsg)) { return 0; } switch (errno) { case EINTR: ... default: fprintf(stderr, "Unrecognized errno %1d in SendReq()\n", errno); break; } return (-1);}What does sizeof() do?2Do You Know What You AreSending To The Network? Computer Communications - CSCI 551 Copyright © William C. ChengIs sizeof(ReqMsg) 11?typedef struct tagReqMsg { unsigned short MsgType; unsigned int Offset; unsigned char ServerDelay; unsigned int DataLen; char *Data;} ReqMsg;3Memory Layout (Cont...) Computer Communications - CSCI 551 Copyright © William C. Cheng0 1 2 3 4 5 10 11 12 13unsigned short usAddrReqMsgType=(unsigned short)0xfe10;request.MsgType = usAddrReqMsgType;request.Offset = 0;request.ServerDelay = 0;request.DataLen = strlen("www.google.com");request.Data = argv[3];Filling the data structure"www.google.com"6 7 8 9this is incorrect4Memory Layout (Cont...) Computer Communications - CSCI 551 Copyright © William C. Chengstream abstraction of TCPint msg_buf_sz=10+strlen("www.google.com")+1;char *msg_buf=(char*)malloc(msg_buf_sz);if (msg_buf == NULL) { fprintf(stderr, "malloc() failed\n"); ... }memset(msg_buf, 0, msg_buf_sz);memcpy(msg_buf, &usAddrReqMsgType, 2); /* is this right? */memcpy(&msg_buf[2], &request.Offset, 4); /* is this right? */msg_buf[6] = &request.ServerDelay, 1;memcpy(&msg_buf[7], &request.DataLen, 4); /* is this right? */strcpy(&msg_buf[11], request.Data); /* is this right? */...free(msg_buf);need to call htons()/htonl() before sending andntohs()/ntohl() after receivingin order to make sure a data object is 2/4 bytes long, youcan use uint16_t/unit32_tthere is really no difference between signed and unsignedexcept in the context of negative numbers, then youneed to watch out for sign extension5TCP’s Stream Abstraction Computer Communications - CSCI 551 Copyright © William C. Cheng0 1 2 3 4 5 6 7 ... 20471st write:MT1 O1 D10 1 2 3 4 5 ... 20472nd write:MT2 O2 D2Receiver concatenates all bytes receivedO1 DL1 D1 DL2 D20 1 2 3 4 5 ... 9103rd write:MT3 O3 D3DL3 D3Need:MT DL D1 D2 D3(Note: Assuming you can write up to 2048 bytes at a time)DL1DL2DL38 9 10 116 7 8 9 10 116 7 8 9 10 11OMT1 O2 O3MT2 MT36 Computer Communications - CSCI 551 Copyright © William C. Cheng TCP’s Stream Abstraction (Cont...)for warmup #1 (and warmup #1 only), you must read andwrite one byte at a timethis means that if you call send() or write() with thefirst argument being a socket descriptor, the 3rdargument must be 17 Computer Communications - CSCI 551 Copyright © William C. Cheng Warmup Project #1ADDR → ADR_REQ3 request typesFILESIZE → FSZ_REQGET → GET_REQclient {adr|fsz|get} [-d delay] [-o offset] \ [-m] hostname:port stringClient program commandline0 1 2 3 4 5 ...Type Offset DataDataLength6 7 8 9 10 11Message formatADR_RPLY3 reply typesFSZ_RPLYGET_RPLYADR_FAILotherFSZ_FAILGET_FAILALL_FAILserver [-t seconds] [-m] portServer program commandlinefor requests, Data came from string in commandline8 Computer Communications - CSCI 551 Copyright © William C. Cheng Examplesclient adr nunki.usc.edu:6001 www.cs.usc.eduADDR<TAB>ADDR = 128.125.3.104client fsz nunki.usc.edu:6001 /etc/passwdFILE_SIZE<TAB>FILESIZE = 1030client get nunki.usc.edu:6001 /bin/lessGET<TAB>FILESIZE = 104908, MD5 = f27df2e0...client get -o 123 nunki.usc.edu:6001 /bin/less<TAB>FILESIZE = 104785, MD5 = eccfd764...openssl md5 /bin/lessMD5(/bin/less)= f27df2e0...9 Computer Communications - CSCI 551 Copyright © William C. Cheng Many RequirementsEx:separate compilationPlease read the spec yourself for detailsbuffer size limitreading and writing one byte at a timebinary file contains binary dataBe careful with binary dataMD5 buffer contains binary datawrite a function to print binary data correctlyif you use "%x" in printf(), the corresponding datais assume to be a signed integerif the most significant bit is 1, will cause sign-extensionSevere pentalty for failing makeSevere pentalty for using large memory buffersSevere pentalty for any segmentation fault -- you must testyour code wellSevere pentalty for not using separate compilation or forhaving all your source code in header files -- you must learn toplan how to write your program10 Computer Communications - CSCI 551 Copyright © William C. Cheng Some Major Requirements for All Projectsrun "top" on nunkiNever do busy-waitdon’t stay in a tight loop and polluse blocking I/O and socketsjust sleep for 50-100 milliseconds before poll againBreak up your code into modulescompile the modules separately, at least one rule permodule per rule in the Makefilea separate rule to link all the modules togetherif your program requites additional libraries, add themto the link stageTo receive full credit for separate compilationto create an executable, at a minimum, you must run thecompiler at least twice and the linker oncefor warmup #1, there are two executables, they can sharemodules11 Computer Communications - CSCI 551 Copyright © William C. Cheng Separate CompilationDon’t design your program "procedurally"You need to learn how to write functions!a function has a well-defined interfacepre-conditionswhat are the meaning of the parameterswhat does it suppose to returnwhat must be true when the function is enteredpost-conditionswhat must be true when the function returnsyou assume that these are trueyou can verify it if you wantyou design your program by making designing a sequenceof function calls12 Computer Communications - CSCI 551 Copyright © William C. Cheng Code Design - Functional vs. Procedural13 Computer Communications - CSCI 551 Copyright © William C. Cheng Sticky Issueswait for all child threads/processes to terminate before theserver terminates itselfmust not kill child threads/processes abruptlyYour server must shutdown gracefullysend signals to child threads/processesa child thread/process must be prepared to handlethis and self-terminatesa child thread/process should react as soon aspossiblesince we are read the socket one byte as a time, youshould


View Full Document

USC CSCI 551 - 09_warmup1-6up

Download 09_warmup1-6up
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 09_warmup1-6up 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 09_warmup1-6up 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?