⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 default.htm

📁 this gives details of the network programming
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>NetProg Homework 1</TITLE><LINK href="../../style/proj.css" type=text/css rel=stylesheet></HEAD><BODY bgColor=white leftMargin=0 topMargin=0 MARGINWIDTH="0" MARGINHEIGHT="0"><TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#8899aa border=0>  <TBODY>  <TR>    <TD class=title>&nbsp; NetProg 2002</TD></TR>  <TR bgColor=black>    <TD class=MENU>&nbsp; &nbsp; <A class=MENU       href="hw1.html#layer1">Layer1</A>       &nbsp; &nbsp; |&nbsp; &nbsp; <A class=MENU       href="hw1.html#layer2">Layer2</A>       &nbsp; &nbsp; |&nbsp; &nbsp; <A class=MENU       href="hw1.html#layer3">Layer3</A>       &nbsp; &nbsp; |&nbsp; &nbsp; <A class=MENU       href="hw1.html#layer4">Layer4</A>       &nbsp; &nbsp; |&nbsp; &nbsp; <A class=MENU       href="hw1.html#sample">Sample       Test Code</A> &nbsp; &nbsp; |&nbsp; &nbsp; <A class=MENU       href="hw1.html#deliv">Deliverables</A>       &nbsp; &nbsp; |&nbsp; &nbsp; <A class=MENU       href="hw1.html#grading">Grading</A>     </TD></TR></TBODY></TABLE><DIV class=page><H2 style="FONT-SIZE: 16pt; COLOR: black; TEXT-ALIGN: center">Homework 1: Layered Half-Duplex Communication Software<BR>Due Date: Friday, Jan 25th (by 11:59PM)<BR></H2><H4 align=center>Submit to <I>netprog-submit@cs.rpi.edu</I> with the subject line "1"<BR>Complete Submission instructions are <A href="submission.html">here</A></H4><HR><P>This homework involves the development of C code to support a <EM>half-duplex</EM> layered communication system. You are provided with the interface protocols (C function prototypes) for each of the layers, your job is to implement these layers - part of this will involve the development of peer-to-peer protocols. You are responsible for providing the C functions that provide layers 2,3 and 4 (described below). You do not need to provide a layer 1 implementation - I've included a sample along with a sample main program. When testing your submission we will use our own layer 1 that may be very different from the one included here - make sure your code does not depend on any specific layer 1 implementation!</P>. <HR><A name=layer1><H3>Layer 1: read/write a single byte.</H3></A><P><B>IMPORTANT:</B> Layer 1 <EM>does</EM> maintain ordering, so that the receiver will receive the first byte sent, before the second byte sent. You can (and should) assume that any layer 1 implementation maintains the order of individual bytes.</P><P>The layer1 interface protocol is specified here, but you don't need to write this layer - just use it. A sample implementation is provided that will support testing of your code - the sample layer will support half-duplex communication using a pipe.</P><P>The layer1 API is shown below - each function returns an int that indicates the number of bytes read/written, in this case the return value should be 1 if everything goes well, -1 indicates some kind of error.</P><P><B><CODE>int l1_write(char b);</CODE></B> <DIV class=in><P>writes the byte specified by <CODE>b</CODE>. Returns 1 on success or -1 on error.</P></DIV><P><B><CODE>int l1_read(char *b);</CODE></B> <DIV class=in><P>Reads one byte and copies the byte to the address specified by <CODE>b</CODE>. Returns 1 on success, -1 on error.</P></DIV><HR><A name=layer2><H3>Layer 2: read/write a <EM>message</EM></H3></A><P>Layer 2 provides transmission/reception of a <EM>message</EM>. A message is simply a sequence of bytes. The important issue here is that there must be some agreement between the sender and the receiver as to what constitutes a <EM>message</EM> (how long is a message?, where is the end?). You must come up with a peer-to-peer protocol and implement the protocol here. The only way for <CODE>l2_write</CODE> to send anything is by using <CODE>l1_write</CODE>, and the only way <CODE>l2_read</CODE> can retreive a byte from the sender is by calling <CODE>l1_read</CODE>.</P><P>The return value of each layer 2 function indicates the length of the message sent or received (in bytes) upon success, or the value -1 to indicate failure. The only assumption you can make about <EM>messages</EM> is that the length is no longer than no longer than 999 bytes. The content of messages can be anything at all, there is no restriction on the values of the indiviual bytes that make up a message.</P><P><B><CODE>int l2_write(char *b,int n);</CODE></B> <DIV class=in><P>Sends a message that consists of the sequence of bytes starting at the address specified as the first parameter (<CODE>b</CODE>) and having length <CODE>n</CODE>. Returns <CODE>n</CODE> on success, -1 means an error occurred. You need to handle all errors that can be detected here, this means you need to check for valid values of <CODE>n</CODE>, and that you check the return value each time you call <CODE>l1_write</CODE>.</P></DIV><P><B><CODE>int l2_read(char *b,int max);</CODE></B> <DIV class=in><P>Reads a message and stores the incoming message starting at the address specified by the pointer. <CODE>b</CODE>. No more than <CODE>max</CODE> bytes will be put in to memory, so <CODE>max</CODE> limits the size of the message read. If a message is received by <CODE>l2_read</CODE> that would require more than <CODE>max</CODE> bytes, <CODE>l2_read</CODE> should return -1 (error). Upon successful reception of a message, the size of the message (the number of bytes stored in <CODE>b</CODE>) is returned.</P><DIV class=in><P><B>NOTE:</B> Make sure that your <CODE>l2_read</CODE> does not allow the sender to overflow the buffer <CODE>b</CODE>! It's not enough to recognize when this <EM>has</EM> happened and return an error, you must not store anything beyond the <CODE>max</CODE><SUP>th</SUP> location of <CODE>b</CODE> (the caller of <CODE>l2_read</CODE> is telling you how large the buffer <CODE>b</CODE> is, you can't assume it's any larger than <CODE>max</CODE>).</P></DIV></DIV><HR><A name=layer3><H3>Layer 3: read/write a message with with error detection</H3></A><P>Layer 3 adds simple error detection to the services provided by layer 2. The function prototypes (which define the interface protocols) looks the same as the layer 3 prototypes, the only difference is that the layer 3 read function should return a -1 (error) if it detects an <EM>error</EM> in the received message. The errors we are looking for here involve transmission errors, we want to try to make sure that the message received is the same as the message that was sent.</P><P><B>Error Detection:</B> The simplest approach is to use a checksum. To use a checksum you simply add together all the bytes of the original message (just treat each byte as a number) and the checksum is the sum modulo 256 (to fit in a single byte). The result is a single byte that can be sent along with the message data, and the receiving end can go through the same steps (computing the checksum) and then compare the received checksum to the computed checksum. If they don't agree - there was an error. If the checksums do agree, it is <EM>likely</EM> there was no error (but it's not a certainty). Many network protocols use checksums to detect errors, although in general they use checksums larger than a single byte to improve the accuracy of the error detection. Feel free to use whatever you want to detect errors, a single byte checksum is just the easiest to implement. When testing your code we will use a <EM>buggy</EM> layer 1 that does introduce errors, so make sure your error detection works!</P><P><B><CODE>int l3_write(char *b,int n);</CODE></B></P><DIV class=in><P>Sends a message of length <CODE>n</CODE>. The bytes sent are specified by the pointer <CODE>b</CODE>. Returns the number of bytes sent on success, -1 means an error.</P></DIV><P><B><CODE>int l3_read(char *b,int max);</CODE></B></P><DIV class=in><P>Reads a message in to memory starting at the address specified by the pointer <CODE>b</CODE>. No more than <CODE>max</CODE> bytes will be put in to memory, so <CODE>max</CODE> must limit the size of the message read. If a message is received by <CODE>l3_read</CODE> that would require more than <CODE>max</CODE> bytes, <CODE>l3_read</CODE> should return -1 (error). Some error detection mechanism is used to detect transmission errors. If such an error is detected by <CODE>l3_read</CODE>, a -1 will be returned. Upon successful reception of a message, the size of the message (the number of bytes stored in <CODE>b</CODE>) is returned.</P></DIV><HR><A name=layer4><H3>Layer 4: read/write a named value</H3></A><P>Layer 4 will provide higher layers with a mechanism for sending and receiving values that have an associated name. The idea is that we could build an application-level protocol that uses layer 4 to send named values between peer processes. For example, a telephone directory service could be built by having clients send a request that could be either a person's name, or phone number; the server could do the appropriate lookup and send back the appropriate result (as a named value).</P><P>Your layer 4 implementation should use layer 3 for sending and receiving messages (nothing else!). Once again, you need to come up with a peer-to-peer protocol so that your layer 4 functions know what to expect and can work together. It doesn't matter what your peer-to-peer protocol is, as long as your <CODE>l4_write</CODE> works with your <CODE>l4_read</CODE>.</P><P><B><CODE>int l4_write(char *name, int namelen, char *value, int valuelen);</CODE></B></P><DIV class=in><P>Sends the named value to the receiver. Returns a 1 on success, -1 means an error.</P></DIV><P><B><CODE>int l4_read(char *name, int *namelenptr, char *value, int *valuelenptr);</CODE></B></P><DIV class=in><P><CODE>l4_read</CODE> uses <CODE>l3_read</CODE> to read a named value and on success returns the value 1, and puts the name of the received value at <CODE>name</CODE> and the value at <CODE>value</CODE>. The pointers <CODE>namelenptr</CODE> and <CODE>valuelenptr</CODE> are reference the sizes of the two buffers when <CODE>l4_read</CODE> is initially called, you must use these values to make sure you don't overflow the buffers. Upon successful return, <CODE>*namelenptr</CODE> should indicate the number of bytes in the <CODE>name</CODE> received and <CODE>*valuelenptr</CODE> should indicate the number of bytes in the <CODE>value</CODE> received. <CODE>l4_read</CODE> must return a -1 on error.</P></DIV><HR><A name=sample><H3>Sample Code and Testing</H3></A><P>Included below is a sample main program that should give you an idea of how to test your code. The code is also available here: <A href="hw1.c">hw1.c</A>. Note that although the code below only accesses the layer 4 functions, it is necessary that your layers are independent and can work with any other implementation of other layers. For example, we might use a different layer 2 implementation with your layer 3 and 4 code - everything should work correctly! Also keep in mind that when we test your code we will generate error conditions (including introducing some simulated transmission errors by providing a buggy layer 1).</P><P>The test code below might be used as part of an application that requires that a username and password be sent from a client to a server. The username is sent followed by the password. The following main program calls layer 4 to send or receive these messages depending on whether it finds any command line arguments present. If there are command line arguments the program assumes it is a <EM>sender</EM> and sends <CODE>argv[1]</CODE> as a username, followed by <CODE>argv[2]</CODE> as a password. If no command line arguments are detected, the program first attempts to read a username using layer 4, then reads an password string, and prints them both out to STDOUT. Using the sample layer 1 code included, you can run the program as a sender and pipe the output of the program to another copy of the program running as a receiver:</P><CENTER><PRE>&gt; ./hw1 username password | ./hw1</PRE><P>The code is also available here: <A href="hw1.c">hw1.c</A></P><TABLE cellPadding=4 bgColor=cyan border=1>  <TBODY>  <TR>    <TD><PRE>// Netprog 2002 HW1 test program.//// This code can be used to test your layer 4 implementation//   (which in turn should use your layer 3 implementation, etc.)//// Included is a set of functions that can be used as a layer 1//   implementation, and a main program.//// Question, suggestions, etc. should be sent to netprog@cs.rpi.edu

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -