Docsity

UNIX Socket Programming - Project | EE 450, Study Guides, Projects, Research of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
EE450 Socket Programming Project, Fall 2007
Due Date: Sunday November 18th, 2007 11:59 AM (Noon)
(The deadline is the same for all on-campus and DEN off-campus students)
Hard deadline (Strictly enforced)
The objective of this assignment is to familiarize you with UNIX socket programming. This assignment is
worth 10% of your overall grade in this course.
It is an individual assignment and no collaborations are allowed. Any cheating will result in an automatic
F in the course (not just in the assignment).
If you have any doubts/questions please feel free to contact the TAs and cc professor Zahid as usual.
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 messenger.cc 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 router.cc 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 (router1.cc, router2.cc, router3.cc) 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
a. You must create 2 concurrent Receivers
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: receiver.c or
receiver.cc or receiver.cpp (all small letters). Also you must call the corresponding
header file (if any) receiver.h (all small letters). You must follow this naming
convention.
ii. Or by running 2 instances of the Router code. However in this case, you probably have 2
pieces of code for which you need to use one of these sets of names: (receiver1.c,
receiver2.c) or (receiver1.cc, receiver2.cc) or (receiver1.cpp, receiver2.cpp) (all small
letters). Also you must call the corresponding header file (if any) receiver.h (all small
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download UNIX Socket Programming - Project | EE 450 and more Study Guides, Projects, Research Electrical and Electronics Engineering in PDF only on Docsity!

EE450 Socket Programming Project, Fall 2007

Due Date: Sunday November 18th, 2007 11:59 AM (Noon)

(The deadline is the same for all on-campus and DEN off-campus students)

Hard deadline (Strictly enforced)

The objective of this assignment is to familiarize you with UNIX socket programming. This assignment is worth 10% of your overall grade in this course. It is an individual assignment and no collaborations are allowed. Any cheating will result in an automatic F in the course (not just in the assignment). If you have any doubts/questions please feel free to contact the TAs and cc professor Zahid as usual.

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 messenger.cc 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 router.cc 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 ( router1.cc, router2.cc, router3.cc) 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 a. You must create 2 concurrent Receivers 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: receiver.c or receiver.cc or receiver.cpp (all small letters). Also you must call the corresponding header file (if any) receiver.h (all small letters). You must follow this naming convention. ii. Or by running 2 instances of the Router code. However in this case, you probably have 2 pieces of code for which you need to use one of these sets of names: ( receiver1.c, receiver2.c ) or ( receiver1.cc, receiver2.cc) or ( receiver1.cpp, receiver2.cpp ) (all small letters). Also you must call the corresponding header file (if any) receiver.h (all small

letters) or receiver1.h, receiver2.h (all small letters). You must follow this naming convention.

4- Input file : message.txt a. There is one input file for this project, message.txt. This input file contains two English sentences. Here is the format of a sample input file:

5 This is the EE450 class 7 We are in the Fall 2007 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. In this example, the number of words in the first sentence is 5 and the number of words in the second sentence is 7. As can be seen the delimiter used between the words is one space. You are given a sample input file message.txt to test your code, however when your project is graded, the TA may use a different input file with the exact same format to test your project. Please note that there are always two lines (hence only two sentences) in this file.

A more detailed explanation of the problem:

This project is divided into 3 phases. 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.

Phase1:

In this phase, Messenger opens the input file and reads the contents of the file (i.e. the two English sentences) and generates some packets using a specific header format (shown in Figure1). Each packet carries (or encapsulates) exactly one English word as its payload or data. For example with the current input file, 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). 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.

Next, Messenger must contact the 3 Routers in a round-robin fashion to distribute these packets among them over TCP sockets. Therefore Messenger must know the TCP port number of each Router in advance. In other words you must hardcode the TCP port number of each Router, in the Messenger code. Table 1 shows how static TCP and UDP port numbers should be defined for each entity in this project.

Messenger will use 3 different dynamically-assigned TCP port numbers (NOT statically defined) and establishes 3 TCP connections to 3 Routers. You are free to implement these 3 TCP connections either in parallel or serially. In other words, Messenger can use its first TCP port number and establish a TCP connection to Router1, send all the necessary packets to Router1and close this connection. It can then repeat this process for Router#2 and Router #3 serially. It’s also possible for Messenger to open 3 concurrent or parallel TCP connections to 3 Routers over 3 different TCP port numbers. This is entirely your choice to implement this part in parallel or serially.

Whether the TCP connections are established serially or in parallel, the packets that are required to be transferred to each Router are pre-determined based on the contents of the input file. For example with the given sample input file, packet #1,#4,#7 and #10 will be sent to Router1, packet #2, #5, #8 and #11 will be sent to Router #2 and packets #3, #6, #9 and #12 will be sent to Router #3. Please note that in this case each Router

Messenger

Router

Router

Router

UDP

CISCO SYSTEMS

3 Com

Receiver1 Receiver

UDP (^) UDP UDP UDP UDP

Static UDP Port =3100+xxx

Dynamic UDP Port

Static UDP Port =3200+xxx

Dynamic UDP Port

Dynamic UDP Port

Figure3: Phase 2, 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).However in the test-case used for grading, the total number of words may not be a multiple of 3, hence some Routers may receive less packets than others. Your code should work for the general case. Figure2 illustrates phase1 of this project.

Phase2:

In phase2 of this project, each Router must inspect the header of each data packet (received from Messenger), correctly identify the destination of each packet and forward them through the correct outgoing interface toward the destination. In this case the outgoing interface corresponds to a UDP socket. Since there are two Receivers in this project, each Router has two outgoing interfaces, in other words you must hardcode the static UDP port for each Receiver inside the Router code. Each Router will then change the identifier string in the packet, from Messenger to its name (i.e. Router1, or Router2 or Router3).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. The Routers can contact the Receivers in any arbitrary order. Upon successful completion of this phase, Receiver1 should have received all the words in sentence#1 and Receiver2 should have received all the words in sentence#2. Figure illustrates the communications over UDP sockets in phase2.

Messenger

Router

Router

Router

UDP

CISCO S YSTEMS

3 Com

Receiver1 Receiver

UDP

Dynamic UDP Port

Static UDP Port =4100+xxx

Dynamic UDP Port

Figure4: Phase 3, Verification

Phase3:

In this phase, each Receiver must assemble the words that it received in phase2, in the correct order and form the original sentence. Each Receiver then generates one packet (according to the provided format in Figure #1) that carries the complete sentence as its payload. It then creates a new UDP socket to communicate with Messenger and sends that packet to Messenger. It is therefore implied that Messenger has a UDP port number that is statically defined and is already hardcoded in both Receiver codes. Messenger will then extract the payload of each packet received from each Receiver, compares it to the corresponding sentence in the input file, e.g. if the packet is received from receiver1 it will be compared to sentence#1 and verifies whether it is a match. It will then announce the results on the screen. This marks the end of Phase3, i.e. the verification.

Table 1: A Summary of Static and Dynamic assignment of TCP and UDP Ports and IP addresses Process Dynamic ports Static Ports IP address obtained from Messenger 3 TCP Ports (Phase1) 1 UDP Port, 4100+ xxx of your ID (Phase3) local host or nunki.usc.edu Router1 1 UDP Port (Phase2) 1 TCP Port, 21100+ xxx of your ID (Phase1) local host or nunki.usc.edu Router2 1 UDP Port (Phase2) 1 TCP Port, 21200+ xxx of your ID (Phase1) local host or nunki.usc.edu Router3 1 UDP Port (Phase2) 1 TCP Port, 21300+ xxx of your ID (Phase1) local host or nunki.usc.edu Receiver1 1 UDP Port (Phase3) 1 UDP Port, 3100+ xxx of your ID (Phase2) local host or nunki.usc.edu Receiver2 1 UDP Port (Phase3) 1 UDP Port, 3200+ xxx of your ID (Phase2) local host or nunki.usc.edu

Assumptions:

  1. The Processes are started in this order: Router, Messenger and receiver.
  2. For each data packet sent over a TCP socket, an Ack packet is returned. An Ack packet is simply the string “ack” (small letters, without the quotes). Like all other messages in this project, a message containing an “ack” must also start with an identifier string.
  3. If you need to have more code files than the ones that are mentioned here, please use meaningful names and all small letters and mention them all in your README file.
  4. You are allowed to use blocks of code from Beej’s socket programming tutorial (Beej’s guide to network programming) in your project.
  5. When you run your code, if you get the message “port already in use” or “address already in use”, 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). If you have to change the port number, please do mention it in your README file.

Requirements:

  1. Do not hardcode the TCP or UDP port numbers that must be obtained dynamically. Refer to Figures 2 through 4 and Table1 to see which ports are statically defined and which ones are dynamically assigned. 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,(struct sockaddr *)&my_addr, (socklen_t *)&addrlen) ; //Error checking if (getsock_check== -1) { perror("getsockname"); exit(1); }

  1. Use gethostbyname() to obtain the IP address of nunki.usc.edu or the local host.
  2. 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.
  3. All the naming conventions and the on-screen messages must conform to the previously mentioned rules.
  4. All messages sent through sockets must start with an identifier string as instructed in the project description.
  5. You are not allowed to pass any parameter or value or string or character as a command-line argument. No user interaction must be required (except for when the user runs the code obviously). Every thing is either hardcoded or dynamically generated as described before.
  1. All the on-screen messages must conform exactly to the project description. You must not add anymore on- screen messages. If you need to do so for the debugging purposes, you must comment out all of the extra messages before you submit your project.
  2. Using fork() or similar system calls are not mandatory if you do not feel comfortable using them to create concurrent processes.
  3. Please do remember to close the socket and tear down the connection once you are done using that socket.

Programming platform and environment:

  1. All your codes must run on nunki (nunki.usc.edu) and only nunki. It is a SunOS machine at USC. You should all have access to nunki, if you are a USC student.
  2. You are not allowed to run and test your code on any other USC Sun machines. This is a policy strictly enforced by ITS and we must abide by that.
  3. No MS-Windows programs will be accepted.
  4. You can easily connect to nunki if you are using an on-campus network (all the user room computers have xwin already installed and even some ssh connections already configured).
  5. If you are using your own computer at home or at the office, you must download, install and run xwin on your machine to be able to connect to nunki.usc.edu and here’s how:

a. Open software.usc.edu in you web browser. b. Log in using your username and password (the one you use to check your USC email). c. Select your operating system (e.g. click on windows XP) and download the latest xwin. d. Install it on your computer. e. Then check the following webpage: http://www.usc.edu/its/connect/index.html for more information as to how to connect to USC machines.

  1. Please also check this website for all the info regarding “getting started” or “getting connected to USC machines in various ways” if you are new to USC: http://www.usc.edu/its/

Programming languages and compilers:

You must use only C/C++ on UNIX as well as UNIX Socket programming commands and functions. Here are the pointers for Beej's Guide to C Programming and Network Programming (socket programming):

http://www.beej.us/guide/bgnet/

(If you are new to socket programming please do study this tutorial carefully as soon as possible and before starting the project)

http://www.beej.us/guide/bgc/

Once you run xwin and open an ssh connection to nunki.usc.edu, you can use a unix text editor like emacs to type your code and then use compilers such as g++ (for C++) and gcc (for C) that are already installed on nunki

a. On nunki.usc.edu, go to the directory which has all your project files. Remove all executable and other unnecessary files. Only include the required source code files and the README file. Now run the following commands:

b. you@nunki>> tar cvf ee450_yourUSCusername_session#.tar * - Now, you will find a file named “ee450_yourUSCusername_session#.tar” in the same directory.

c. you@nunki>> gzip ee450_yourUSCusername_session#.tar – Now, you will find a file named “ee450_yourUSCusername_session#.tar.gz” in the same directory.

d. Transfer this file from your directory on nunki.usc.edu to your local machine. You need to use an FTP program such as CoreFtp to do so. (The FTP programs are available at software.usc.edu and you can download and install them on your windows machine.)

  1. Upload “ee450_yourUSCusername_session#.tar.gz” to the Digital Dropbox (available under Tools) on the DEN website. After the file is uploaded to the dropbox, you must click on the “ send” button to actually submit it. If you do not click on “ send ”, the file will not be submitted.
  2. Right after submitting the project, send a one-line email to your designated TA (NOT all TAs) informing him or her that you have submitted the project to the Digital Dropbox. Please do NOT forget to email the TA or your project submission will be considered late and will automatically receive a zero.
  3. You will receive a confirmation email from the TA to inform you whether your project is received successfully, so please do check your emails well before the deadline to make sure your attempt at submission is successful.
  4. You must allow at least 12 hours before the deadline to submit your project and receive the confirmation email from the TA.
  5. By the announced deadline all Students must have already successfully submitted their projects and received a confirmation email from the TA.
  6. Please take into account all kinds of possible technical issues and do expect a huge traffic on the DEN website very close to the deadline which may render your submission or even access to DEN unsuccessful.
  7. Please do not wait till the last 5 minutes to upload and submit your project because you will not have enough time to email the TA and receive a confirmation email before the deadline.
  8. Sometimes the first attempt at submission does not work and the TA will respond to your email and asks you to resubmit, so you must allow enough time (12 hours at least) before the deadline to resolve all such issues.

11. You have plenty of time to work on this project and submit it in time hence there is

absolutely zero tolerance for late submissions! Do NOT assume that there will be a late

submission penalty or a grace period. If you submit your project late (no matter for

what reason or excuse or even technical issues), you simply receive a zero for the

project.

Grading Criteria:

Your project grade will depend on the following:

  1. Correct functionality, i.e. how well your programs fulfill the requirements of the assignment, specially the communications through UDP and TCP sockets.
  2. Inline comments in your code. This is important as this will help in understanding what you have done.
  3. Whether your programs work as you say they would in the README file.
  4. Whether your programs print out the appropriate error messages and results.
  5. If your submitted codes, do not even compile, you will receive 10 out of 100 for the project.
  6. If your submitted codes, compile but when executed, produce runtime errors without performing any tasks of the project, you will receive 10 out of 100.
  7. If your codes compile but when executed only perform phase1 correctly, you will receive 40 out of 100.
  8. If your codes compile but when executed perform only phase1 and phase 2 correctly, you will receive 80 out of 100.
  9. If your code compiles and performs all tasks in all 3 phases correctly and error-free, and your README file conforms to the requirements mentioned before, you will receive 100 out of 100.
  10. If you forget to include any of the code files or the README file in the project tar-ball that you submitted, 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.)
  11. If your code does not correctly assign the TCP or UDP port numbers dynamically (in any phase), you will lose 20 points.
  12. You will lose 5 points for each error or a task that is not done correctly.
  13. The minimum grade for an on-time submitted project is 10 out of 100.
  14. There are no points for the effort or the time you spend working on the project or reading the tutorial. If you spend about 2 months on this project and it doesn’t even compile, you will receive only 10 out 0f 100.
  15. 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.
  16. If you submit a makefile or a script file along with your project that helps us compile your codes more easily, you will receive 5 bonus points.
  17. The maximum points that you can receive for the project with the bonus points is 100. In other words the bonus points will only improve your grade if your grade is less than 100.