Vous êtes sur la page 1sur 5

18-345 Introduction to Telecommunication Networks

Project 1: Socket Programming


September 7, 2007 Due: September 21, 2007

Project Goal
This project will give you some hands-on experience with socket programming in C. You will use UNIX/LINUX sockets to implement simple client and server programs that interact with each other to accomplish a file transfer in a connectionless manner. Before starting this project, you should read Section 2.4 The Berkeley API of your text-book, Communication Networks.

Project Assignment
In this project, you need to implement a server that opens a socket and listens for incoming data transfer at a user-specified port number. You also need to implement a client that reads a binary file from the file system and transfers the file to the server. When the server receives the clients data, it writes this data to a file.

Packet Format
All packets sent between the client and the server should have the following structure:
struct packet { unsigned int type; unsigned int seqnum; unsigned int length; char data [500]; }

The type field indicates the type of packet that is being transmitted. There are two types of packets in this project: data packets in which the type field is set to 0, and end-of-transmission (EOT) packets in which the type field is set to 1. The seqnum field indicates the sequence number of the packet. Sequence numbers begin at 0 and increase by one for each packet that is sent.

The length field should be set to the length of the data when the packet is a data packet. Therefore, it should be in the range of 1 to 500. For EOT packets, the length should be set to zero. The data field contains the data to be transmitted. This might be the content of the whole file, or a fragment of the file to be transferred if the file size is larger than 500 bytes.

Client Program (sender.c)


You should implement a sender program in C, named sender.c, which works on a UNIX and LINUX system. Its command line input should be as follows:
sender <server address> <server port number> <name of the file to be transferred>

Upon execution, the sender program read data from the file specified in the third parameter and sends it to the server (also referred to as the receiver) via a UDP1 socket. After all contents of the file have been transmitted to the receiver, the sender should send an EOT packet to the receiver. The EOT packet is in the same format as a regular data packet, except that its type field must be set to 1 and its length is set to zero. The seqnum field should be the next number in sequence. Note that if the size of transferring file is greater than 500 bytes, it will not fit in one packet (since the data field in a UDP packet can store only 500 bytes.) In this case, you need to split the file into fragments with maximum size of 500 and send each fragment to the server. This can be done by iteratively reading 500 bytes at a time and sending it to the server until all data has been read and sent. For this project assume that the file size will not be too large for the sequence numbers to overflow. In order to avoid buffer overflowing or network congestion, you should allow your client to sleep at least one second between consecutive packet transmissions. You should also test what would happen if the client does not sleep after each transmission.

UDP (User Datagram Protocol) provides an unreliable, block-based, best-effort packet transmission. The order and delivery of packets are not guaranteed. This means your packets might occasionally get lost or re-ordered at the receiver, even though that is extremely rare in local networks. You need to keep track of this using sequence numbers. We will deal with recovering from such errors in later projects.

Server Program (receiver.c)


You should implement a server program, named receiver.c, in C on a UNIX and LINUX system. Its command input should be as follows:
receiver <UDP port number to be listened> <name of the file to be created>

Upon execution, the server should open a UDP socket and listen at the specified port number. It should also create and open a file with the specified name. When receiving packets sent by the sender via the UDP socket, it should examine the packets seqnum & type fields. Use the sequence number field to track if any packets are missing or out of order. If it is a data packet, then the server should write its data to the opened file. Packets should be written to the file in the order in which they are received. On the other hand, if it is an EOT packet, the server should close the UDP connection as well as the file handler. Before closing the connection, the server should display whether the transfer was successful, or whether any packets were missing and/or reordered. There is no need to display the packet numbers of missing or reordered packets. After this the server should terminate. You need not take care of the following: Error recovery from lost or out of order packets. Sequence number overflow/wraparound

Makefile
You should also prepare a makefile that generates the executable file sender from sender.c and the executable file receiver from receiver.c. Note: Please make sure you submit a makefile. We will be taking off points (10%) if you do nott submit the project with a makefile. Also note that printing individual gcc statements in different files doesnt count as making a makefile. You must write a single file with all the compilation statements in it.

Example of Execution
Please output relevant information regarding the execution of your program. For instance, assume you are sending a file called src.jpg from unix43.andrew.cmu.edu to unix2.andrew.cmu.edu. After a successful execution, the receiver will generate a file called dest.jpg which will be a copy of the original file. Here is an example of successful and clear program output: On the host unix2.andrew.cmu.edu
% ./receiver 2312 dest.jpg Initializing receiver on port 2312 Created file dest.jpg Start listening... Received packet: [Num=0][Type=0][Size=500]... Received packet: [Num=1][Type=0][Size=500]... Received packet: [Num=4][Type=0][Size=399]... Received packet: [Num=2][Type=0][Size=500]...

Finished! Finished! Finished! Finished!

Received packet: [Num=5][Type=1][Size=0]... Received EOT packet! Reordering = Yes Losses = Yes Terminating

On the host unix43.andrew.cmu.edu


% ./sender unix2.andrew.cmu.edu 2312 src.jpg Initializing sender, sending to unix2.andrew.cmu.edu on port 2312 Sending packet: [Num=0][Type=0][Size=500] Sending packet: [Num=1][Type=0][Size=500] Sending packet: [Num=2][Type=0][Size=500] Sending packet: [Num=3][Type=0][Size=500] Sending packet: [Num=4][Type=0][Size=399] Sending EOT packet. Terminating.

Turn In Procedure
Pack all of your files into a single compressed file and name it p1ANDREWID.zip, where p1 stands for project 1 and "ANDREWID" is your Andrew ID (e.g., p1mpimente.zip). Please remember these points while zipping the files: 1. DO NOT zip up the directory you were working in. Just zip up the files you were working on. 2. DO NOT copy and paste your code into .txt files. Please submit the actual files that you were working on. (for example, *.c files and the Makefile) 3. Please double check that you zip and submit the right files. Also please double check that your files have not been corrupted by zipping process. You will be submitting your project via Blackboard. To submit the project, go to Assignments Projects Project 1 View/Complete assignment (If you dont see this project created yet, please wait a few days for it to be created. The link will be created well before you need to submit the project) Then SUBMIT your file (and comments if you want to) by clicking "submit." NOTE: If you click "save," it will just save your file; we will not be able to access/grade your file.

Demos
To help ensure that you understand what you have done, you will have to demo your program to a TA and answer some questions about it. Details regarding demo time will be provided at a later date. Please show up for your demo on time. Failure to show up for your demo will result in a project grade reduction of 20% (assuming you eventually show up).

Notes
To make sure your program works on both UNIX and LINUX machines, you should test your code on sun4.andrew.cmu.edu (SunOS = UNIX) and linux.andrew.cmu.edu (Linux) and have your code executed on both platforms. Your program should be able to send packets from a UNIX system to a LINUX one, and vice versa. You can use the uname command to view system and network information. For instance:
% uname Linux % uname -n unix43.andrew.cmu.edu

When compiling codes on a UNIX machine, make sure to include -lsocket -lnsl. For example, executing the makefile with compiling instructions for UNIX would show:
% make unix gcc -o sender sender.c -lsocket -lnsl gcc -o receiver receiver.c -lsocket lnsl

All includes of socket library functions can be obtained from Sec. 2.4 of your textbook. It is extremely unlikely that you will see packet reordering on the local CMU network. However, you can easily test whether your code to detect packet reordering works by having the sender reorder packets. Packet loss is possible, especially if the sender does not sleep, but you can also test it with a modified sender if needed. Please use the discussion board on Blackboard for any project related queries/doubts.

Evaluation
The following scheme of partial credit will be used for grading. Points 10 10 40 20 20 100

Correct project submission (including makefile) Error-free compilation of source codes & proper order and type of arguments for client & server programs Complete, untyped, error-free transfer of a file between two hosts in UNIX and LINUX using your sender & receiver Complete transfer of file between 2 hosts using our test sender/receiver at one end & your program at the other Correct answers to questions asked during demo Total

Vous aimerez peut-être aussi