You are on page 1of 12

Internet Engineering Course Socket Programming Project Spring 2011

Adapted from: Mr. Viney Khera’s course project at University of Southern California Instructor: Dr. Mohammad Nassiri Bu-Ali Sina University, Hamedan

The objective of this assignment is to familiarize you with UNIX socket programming. This assignment is worth 25% of your overall grade in this course. You have to work as a team of 2 students to do this programming assignment. Any cheating will result in an automatic F in the course (not just in the assignment).

The Problem:
In this project you will be simulating transfer of fragmented messages over TCP and UDP sockets in a network with client-server architecture. The project has 3 major phases: Fragmentation/distribution, Routing and Verification. In Phase 1, all communications are through TCP sockets. In phases 2 and 3 however, all communications are over UDP sockets.

Code and Input files:
You must write your programs either in C or C++ on UNIX. In fact, you will be writing (at least) 3 different pieces of code: 1- Messenger a. You must use one of these names for this piece of code: messenger.c or or messenger.cpp (all small letters). Also you must call the corresponding header file (if any) messenger.h (all small letters). You must follow this naming convention. 2- Router a. You must create 3 concurrent Routers i. Either by using fork() or a similar Unix system call. In this case, you probably have only one piece of code for which you need to use one of these names: router.c or or router.cpp (all small letters). Also you must call the corresponding header file (if any) router.h (all small letters). You must follow this naming convention. ii. Or by running 3 instances of the Router code. However in this case, you probably have 3 pieces of code for which you need to use one of these sets of names: (router1.c, router2.c, router3.c) or (,, or (router1.cpp, router2.cpp and router3.cpp) (all small letters). Also you must call the corresponding header file (if any) router.h (all small letters) or router1.h, router2.h and router3.h (all small letters). You must follow this naming convention. 3- Receiver

e. receiver2.h (all small letters) or however when your project is graded. It is not possible to proceed to one phase without completing the previous phase and in each phase you will have multiple concurrent processes that will communicate either over TCP or UDP sockets.txt a. Therefore Messenger must know the TCP port number of each Router in advance. Here is the format of a sample input file: 5 This is the IE892 class 7 We are in the Spring 2011 semester In this sample input file each line starts with a number between 2 and 8 which corresponds to the number of words in the sentence on that line. the two English sentences) and generates some packets using a specific header format (shown in Figure1). You must follow this naming convention. The packets that carry words of sentence#1 are destined for Receiver#1 and packets that carry words of sentence#2 are destined for Receiver #2. A more detailed explanation of the problem: This project is divided into 3 phases. you probably have 2 pieces of code for which you need to use one of these sets of names: (receiver1. 4. Messenger must contact the 3 Routers in a round-robin fashion to distribute these packets among them over TCP sockets. This input file contains two English sentences. Table 1 shows how static TCP and UDP port numbers should be defined for each entity in this project. in the Messenger code. receiver2. receiver2. Please note that there are always two lines (hence only two sentences) in this file. Either by using fork() or a similar Unix system call. Messenger will create a total of 12 packets (5 packets for the words in sentence #1 and 7 packets for the words in sentence#2). Messenger will use 3 different dynamically-assigned TCP port numbers (NOT statically defined) and establishes 3 TCP connections to 3 Routers. You are given a sample input file message.txt.c. the TA may use a different input file with the exact same format to test your project.Input file : message.a. you probably have only one piece of code for which you need to use one of these names: or (receiver1. Or by running 2 instances of the Router code. You must follow this naming convention. In this example. Also you must call the corresponding header file (if any) receiver. Phase1: In this phase.h (all small letters). In this case. message.cpp (all small letters).c or receiver. In other words you must hardcode the TCP port number of each Router. You are free to implement these 3 TCP connections either in . As can be seen the delimiter used between the words is one space. receiver2. You must create 2 concurrent Receivers i.cpp) (all small letters). For example with the current input file. There is one input file for this project. Messenger opens the input file and reads the contents of the file (i.c) or (receiver1.txt to test your code. Also you must call the corresponding header file (if any) or receiver. ii. the number of words in the first sentence is 5 and the number of words in the second sentence is 7.cpp. Each packet carries (or encapsulates) exactly one English word as its payload or data. However in this case.h (all small letters). Next.

This is entirely your choice to implement this part in parallel or serially. Whether the TCP connections are established serially or in parallel. It’s also possible for Messenger to open 3 concurrent or parallel TCP connections to 3 Routers over 3 different TCP port numbers. packet #1. Please note that in this case each Router . the packets that are required to be transferred to each Router are pre-determined based on the contents of the input file. #5. send all the necessary packets to Router1and close this connection.#7 and #10 will be sent to Router1. #6. It can then repeat this process for Router#2 and Router #3 serially.#4. #9 and #12 will be sent to Router #3. Messenger can use its first TCP port number and establish a TCP connection to Router1. packet #2. In other words. For example with the given sample input file.parallel or serially. #8 and #11 will be sent to Router #2 and packets #3.

Router3. Fragmentation and distribution of fragments . Receiver1. Receiver2 1 or 2 The word Total number number in Of words in this sentence this sentence Either a single English word or a complete English Sentence (a) Messenger 1 4 5 IE892 (b) In this example word#4 in sentence#1 with a total of 5 words is encapsulated in a packet sent from Messenger to Receiver1 This is the IE892 class Figure1: (a) General format of a message (b) An example Messenger TCP Dynamic TCP Port #1 Dynamic TCP Port #2 TCP Dynamic TCP Port #3 TCP Static TCP Port = 21100+xxx CISCOSYSTEMS Router1 Static TCP Port = 21300+xxx Router2 3Com Static TCP Port = 21200+xxx Receiver1 Receiver2 Figure2: Phase 1. Router1.Router2.Identifier String Sentence# Fragment# Total Payload Any of the following strings: Messenger.

The Routers can contact the Receivers in any arbitrary order. each Router has two outgoing interfaces. in other words you must hardcode the static UDP port for each Receiver inside the Router code. Routing receives the same number of packets because there are a total of 12 English words in the input file and 12 is evenly divisible by 3 (the number of Routers). Phase2: In phase2 of this project. or Router2 or Router3).Messenger Router1 CISCOSYSTEMS Router3 Dynamic UDP Port Dynamic UDP Port UDP UDP Receiver1 UDP Router2 3Com Dynamic UDP Port UDP Receiver2 UDP UDP Static UDP Port =3100+xxx Static UDP Port =3200+xxx Figure3: Phase 2. Figure3 illustrates the communications over UDP sockets in phase2. Router1. However in the test-case used for grading.e. . each Router must inspect the header of each data packet (received from Messenger).It must then create a UDP socket to communicate with a Receiver and send the packets that are destined for that Receiver over that UDP socket. Your code should work for the general case. Each Router will then change the identifier string in the packet. Receiver1 should have received all the words in sentence#1 and Receiver2 should have received all the words in sentence#2. In this case the outgoing interface corresponds to a UDP socket. the total number of words may not be a multiple of 3. Since there are two Receivers in this project. Figure2 illustrates phase1 of this project. correctly identify the destination of each packet and forward them through the correct outgoing interface toward the destination. from Messenger to its name (i. hence some Routers may receive less packets than others. Upon successful completion of this phase.

Each Receiver then generates one packet (according to the provided format in Figure #1) that carries the complete sentence as its payload. Messenger will then extract the payload of each packet received from each Receiver. 21200+ xxx of your ID (Phase1) local host Router3 1 UDP Port (Phase2) 1 TCP Port. 21300+ xxx of your ID (Phase1) local host Receiver1 1 UDP Port (Phase3) 1 UDP Port. if the packet is received from receiver1 it will be compared to sentence#1 and verifies whether it is a match. 21100+ xxx of your ID (Phase1) local host Router2 1 UDP Port (Phase2) 1 TCP Port. in the correct order and form the original sentence. e. It then creates a new UDP socket to communicate with Messenger and sends that packet to Messenger. 3100+ xxx of your ID (Phase2) local host Receiver2 1 UDP Port (Phase3) 1 UDP Port. It will then announce the results on the screen. It is therefore implied that Messenger has a UDP port number that is statically defined and is already hardcoded in both Receiver codes. each Receiver must assemble the words that it received in phase2. 3200+ xxx of your ID (Phase2) local host .e.g. Verification Phase3: In this phase.Messenger UDP CISCOSYSTEMS Static UDP Port =4100+xxx Router1 Router3 UDP Router2 3Com Receiver1 Receiver2 Dynamic UDP Port Dynamic UDP Port Figure4: Phase 3. the verification. Table 1: A Summary of Static and Dynamic assignment of TCP and UDP Ports and IP addresses IP address Process Dynamic ports Static Ports obtained from Messenger 3 TCP Ports (Phase1) 1 UDP Port. compares it to the corresponding sentence in the input file. This marks the end of Phase3. i. 4100+ xxx of your ID (Phase3) local host Router1 1 UDP Port (Phase2) 1 TCP Port.

your codes must print out the following messages on the screen as listed in the following Tables.On-screen Messages: In order to clearly understand the flow of the project. and IP address Messenger is now connected to <Router#> Sent <packet#> containing <payload> to <Router#> over <socket type> Completed transfer of <number of packets> Packets to <Router#> Received Ack for <packet#> from <name of sender> over <socket type> Received The Sentence <payload> from <Receiver#> over <socket type> Sentence# is a match End of Phase# for Messenger Table 3: Router on-screen messages Event Upon Startup On-screen Message <Router#> has TCP Port …… UDP Port …. Table 2: Messenger on-screen messages Event Upon Startup Upon establishing a connection to each Router Sending a data Packet to a Router Upon sending all the required packets to a Router Receiving an ack Packet from a Router Receiving a data Packet from a Receiver Upon matching sentence# with the one in the input file End of Phase1 and Phase3 On-screen Message Messenger has TCP Ports …… UDP Port …. and IP address Received <packet#> containing <payload> from Messenger over Receiving a data Packet from <socket type> Messenger Upon accepting a connection <Router#> accepted connection request from Messenger request from Messenger Forwarded <packet#> containing <payload> to <Receiver#> over Sending a data Packet to a <socket type> Receiver Completed transfer of <number of packets> Packets to Upon sending all the required <Receiver#> packets to a receiver End of Phase1 and Phase2 End of Phase# for <Router#> .

For each data packet sent over a TCP socket. please use meaningful names and all small letters and mention them all in your README file. If you need to have more code files than the ones that are mentioned here. please first check to see if you have a zombie process (from past logins or previous runs of code that are still not terminated and hold the port busy). If you do not have such zombie processes or if you still get this message after terminating all zombie processes. try changing the static UDP or TCP port number corresponding to this error message (all port numbers below 1024 are reserved and must not be used). You are allowed to use blocks of code from Beej’s socket programming tutorial (Beej’s guide to network programming) in your project. 5. a message containing an “ack” must also start with an identifier string. Messenger and receiver. //Error checking if (getsock_check== -1) { perror("getsockname"). an Ack packet is returned. without the quotes). 4. Use getsockname() function to retrieve the locally-bound port number wherever ports are assigned dynamically as shown below: //Retrieve the locally-bound name of the specified socket and store it in the sockaddr structure getsock_check=getsockname(TCP_Connect_Sock. exit(1). 3. Like all other messages in this project. please do mention it in your README file. An Ack packet is simply the string “ack” (small letters. Requirements: 1. Do not hardcode the TCP or UDP port numbers that must be obtained dynamically. The Processes are started in this order: Router. When you run your code. (socklen_t *)&addrlen) . and IP address Received <packet#> containing <payload> from<Router#> over <socket type> Forwarded sentence# to Messenger over <socket type> End of Phase# for <Receiver#> 1.Event Upon Startup Receiving a data Packet from a Router Sending a data Packet to Messenger End of Phase2 and Phase3 Assumptions: Table 4: Receiver on-screen messages On-screen Message <Receiver#> has UDP Ports ….(struct sockaddr *)&my_addr. Refer to Figures 2 through 4 and Table1 to see which ports are statically defined and which ones are dynamically assigned. If you have to change the port number. 2. } . if you get the message “port already in use” or “address already in use”.

c or yourfile. You are not allowed to pass any parameter or value or string or character as a command-line argument. Use gethostbyname() to obtain the IP address of the local host. All the on-screen messages must conform exactly to the project description. It will make an executable by the name of "yourfileoutput”. 6.cpp. 5. you must comment out all of the extra messages before you submit your project. 9. Here are the pointers for Beej's Guide to C Programming and Network Programming (socket programming): http://www. Please do remember to close the socket and tear down the connection once you are done using that socket. 8. gcc -o yourfileoutput yourfile. All messages sent through sockets must start with an identifier string as instructed in the project description. You must use the following commands and switches to compile yourfile. Using fork() or similar system calls are not mandatory if you do not feel comfortable using them to create concurrent processes. 4.beej.cpp -lsocket -lnsl -lnsl -lresolv -lresolv Do NOT forget the mandatory naming conventions mentioned before! Also inside your code you need to include these header files in addition to any other header file you think you may need: #include <stdio. 3. No user interaction must be required (except for when the user runs the code obviously).h> #include <unistd. If you need to do so for the debugging purposes. Programming languages and compilers: You must use only C/C++ on UNIX as well as UNIX Socket programming commands and functions. 7.c -lsocket g++ -o yourfileoutput yourfile. You must not add anymore onscreen messages. You can either terminate all processes after completion of phase3 or assume that the user will terminate them at the end by pressing ctrl-C.h> #include < (If you are new to socket programming please do study this tutorial carefully as soon as possible and before starting the project) http://www. Every thing is either hardcoded or dynamically generated as described before. All the naming conventions and the on-screen messages must conform to the previously mentioned rules.beej.h> .2. To compile your You can use a unix text editor like emacs (or vi/vim) to type your code and then use compilers such as g++ (for C++) and gcc (for C).

What the TA should do to run your programs. c. Now run the following commands: b.tar. go to the directory which has all your project files. Remove all executable and other unnecessary files. tar cvf ie892_username1_username2_univ.h> #include <string. (Any specific order of events should be mentioned.tar – Now.) f. In this file write a.h> Submission Rules: 1.g. say what functions and where they're from.tar.Your Student IDs c. (Also identify this with a comment in the source code. On your host. my file name would be ie892_nassiri_basu.#include <errno.Now. if any. how well your programs fulfill the requirements of the assignment. i. e. Along with your code files.h> #include <netinet/in. say so.h> #include <arpa/inet. h.Your Full Names as given in the class list b.tar” in the same directory. Only include the required source code files and the README file.gz” in the same directory. you will find a file named “ie892_username1_username2_univ. It should say under what conditions the project fails. What your code files are and what each one of them does.gz (all small letters) e.) Submissions WITHOUT README files WILL NOT BE GRADED. The format of all the messages exchanged (other than what is mentioned in the project description). include a README file.h> #include <netdb. (Please do not repeat the project description. Any idiosyncrasy of your project.h> #include <sys/wait. g.h> #include <sys/types. If so.h> #include <sys/socket.e. specially the .gz. What you have done in the assignment d. gzip ie892_username1_username2_univ. Reused Code: Did you use code from anywhere for your project? If not. Compress all your files including the README file into a single “tar ball” and call it: IE892_username1_username2_univ.tar. Grading Criteria: Your project grade will depend on the following: 1.tar * . Here are the instructions: a. you will find a file named “ie892_username1_username2_univ. just name your code files and briefly mention what they do). 2. Correct functionality.

If your codes compile but when executed perform only phase1 and phase 2 correctly. If your codes compile but when executed only perform phase1 correctly. you will receive 40 out of 100. This is important as this will help in understanding what you have done. you will lose 20 points. If you forget to include any of the code files or the README file in the project tar-ball that you submitted. If you submit a makefile or a script file along with your project that helps us compile your codes more easily. Your code will not be altered in any ways for grading purposes and however it will be tested with different input files. If your submitted codes. There are no points for the effort or the time you spend working on the project or reading the tutorial. 6. you will receive 100 out of 100. do not even compile. Inline comments in your code. 3. 5. 17.) 11. 2. 13.communications through UDP and TCP sockets. 16. If your code compiles and performs all tasks in all 3 phases correctly and error-free. according to the project description and your README file and then check whether it works correctly or not. In other words the bonus points will only improve your grade if your grade is less than 100. you will receive only 10 out 0f 100. and your README file conforms to the requirements mentioned before. you will receive 5 bonus points. 9. You will lose 5 points for each error or a task that is not done correctly. compile but when executed. The minimum grade for an on-time submitted project is 10 out of 100. If you spend about 2 months on this project and it doesn’t even compile. you will receive 10 out of 100. If your submitted codes. Whether your programs work as you say they would in the README file. 10. 8. Using fork() or similar system calls are not mandatory however if you do use fork() or similar system files in your codes to create concurrent processes (or threads) and they function correctly you will receive 10 bonus points. 7. 14. Whether your programs print out the appropriate error messages and results. 4. 12. 18. 15. you will lose 5 points for each missing file (plus you need to send the file to the TA in order for your project to be graded. The maximum points that you can receive for the project with the bonus points is 100. If your code does not correctly assign the TCP or UDP port numbers dynamically (in any phase). Your designated TA runs your project as is. Cautionary Words: . produce runtime errors without performing any tasks of the project. you will receive 80 out of 100. you will receive 10 out of 100 for the project.

3. Copying code from friends is called plagiarism not collaboration and will result in an F for the entire course. All programs will be compared with automated tools to detect similarities. It is strongly recommended that students develop their code on a linux-based host like Ubuntu. Identify the zombie processes and their process number and kill them by typing at the command-line: 5. examples of code copying will get an F for the course. . must be listed in your README file. Any libraries or pieces of code that you use and you did not write. Academic Integrity: All students are expected to write all their code on their own. “I didn’t know” is not an excuse. In view of what is a recurring complaint near the end of a project. please make sure you kill them every time you want to run your code. You may create zombie processes while testing your codes. otherwise they hold the assigned port numbers and sockets busy. Kill -9 processnumber 6. we want to make it clear that the target platform on which the project is supposed to run is a linux-based host. To see a list of all zombie processes even from your past logins. Start on this project early!!! 2. Please do remember to terminate all zombie or background processes.1. try this command: ps –aux | grep <your_username> 4.