----------------------- Lab #01 for CST8165 due September 6, 2006 ----------------------- -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Global weight: 2% of your total mark this term. Due date: before 23h59 Wednesday September 6 The deliverables for this exercise are to be submitted online in the Linux Lab T127 using the "cstsubmit" method described in the exercise description, below. No paper; no email; no FTP. Late-submission date: I will accept without penalty exercises that are submitted late but before 12h00 (noon) on Thursday, September 7. After that late-submission date, the exercise is worth zero marks. Exercises submitted by the *due date* will be marked online and your marks will be sent to you by email after the late-submission date. Exercise Synopsis: Copy (with proper credit to the original author and source) a TCP server and client from the given sources. Modify them. Test them. Where to work: Submissions must compile and run cleanly in the T127 Linux Lab, though you are free to work on them anywhere you like. Recall that most any program or C function has a manual page: RTFM Coding client/server -------------------- 1) Read the beginning of the Sockets Tutorial - http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html The Server 2) Copy the code for the second C server "server2.c" from the section "Enhancements to the server code". Credit your sources in comments in the code after you have copied it. (No Plagiarism!) 3) Compile it: $ gcc -Wall server.c Fix the compile warnings (gcc -Wall) by adding the missing header files - use the man pages to find the missing header lines for each function - fix the warning about signed/unsigned use of int 4) Add this line to main(): alarm(30*60); /* kill program after 30 minutes */ - this is in case you forget to kill the program yourself when you leave! - to see your background processes on Linux/Unix, use command: ps gx 5) Add code to check the return code of "listen()" and print an error on stderr if it fails. (You may use use perror().) 6) Fix "The zombie problem" using the AIX-style fix from the Tutorial. 7) Verify that your program works using the standard TCP client netcat: $ nc -v localhost e.g. you would start your server in the background and then connect: $ ./server 5000 & $ nc -v localhost 5000 The Client 8) Copy the bidirectional Perl "Interactive Client with IO::Socket" from: http://www.perl.com/doc/manual/html/pod/perlipc.html Credit your sources in comments in the code after you have copied it. 9) Remove any spaces in front of the first line: #!/usr/bin/perl -w - the first line must be flush left with no space or blank line above - make the client file executable 10) Execute the client and use it to connect to the C server you built above: - use your new Perl client instead of netcat: $ ./server 5000 & $ ./client.pl localhost 5000 If you want the client to exit on EOF from STDIN, it has to send EOF to the server so that the server exits and sends EOF back to the client. To close the socket in the client so the server sees EOF, add the line "shutdown($handle, 1);" after the client sees EOF on STDIN. (See the shutdown function in "man perlfunc" for why this works.) The Modifications 11) Pick up the setsockopt SO_REUSEADDR code fragment from section 4.2 of http://beej.us/guide/bgnet/output/htmlsingle/bgnet.html and add it to your server. This will make the server re-use socket ports nicely and avoid "Address already in use". 12) Modify the server child process to loop reading lines from the client until it sees EOF (zero bytes read) or an error on the socket, then exit. Have the child print a message when it exits. Distinguish between EOF and error in the message(s). 13) Modify the server child process to send the bytes read from the client, back to the client via the socket. When this is working, you can delete the "Here is the message" and "I got your message" print lines. The client will receive the data back from the server and echo it. 14) Exit with an error if the number of bytes actually written to the socket (as returned from write()) does not match the number you requested to be sent. (This isn't really an error; we'll fix this code later.) 15) Fix the buffer string handling in the child to avoid use of bzero. (It is wasteful to zero out the entire buffer. Fix it.) The Tests 16) Start a "script" session (man script) with output file "testing.txt". Start your server. First use "netcat" to connect to your server and demonstrate that input from the keyboard goes to your server and returns. Next, run your Perl client, and demonstrate the same thing. Exit the script session. Questions Copy these questions into a file "answers.txt" and answer each one below the question itself: 1) How many bytes does it take to store a dotted quad IP address? 2) What range of decimal values can each of the four numbers take in a dotted quad? What are those values in hexadecimal? in binary? 3) How many bytes does it take to store a TCP/UDP port number? 4) Show how the IP dotted quad 1.2.3.4 would appear in memory on (a) a Motorola (Big Endian) computer, (b) an Intel (Little Endian) computer, (c) on a network (in network byte order). Clearly show the addresses of each byte. Reference: http://www.cs.rpi.edu/courses/sysprog/sockets/byteorder.html 5) True/False: each ASCII character has the same bit pattern on both Big-Endian and Little-Endian computers. 6) True/False: each Latin-1 (ISO8859-1) character has the same bit pattern on both Big-Endian and Little-Endian computers. 7) True/False: each Unicode character has the same bit pattern on both Big-Endian and Little-Endian computers. 8) True/False: the decimal number 257 has the same bit pattern on both Big-Endian and Little-Endian computers. Submission ---------- Submission Standards: 1. At the top of each and every submitted file, as comments, create an Exterior Assignment Submission label following the example you will find under the "Assignment Standards" button on my teaching home page teaching.idallen.com . (The Teaching home page is not the same as the Course home page.) For full marks, follow the directions for the label exactly. The label has exactly 7 lines, plus an optional Comments line. The spelling of the label fields on the seven lines must be exactly as shown (machine readable). The spelling must be exact. Exact! 2. For material you copy from other sources, credit the author and source. Failure to acknowledge the source of copied material and claiming you wrote it yourself will constitute academic fraud. 3. Using the cstsubmit command: Reference Class Notes file: cstsubmit.txt Submit four files for marking as Exercise 01 using the following *single* cstsubmit command line: $ ~alleni/bin/cstsubmit 01 server2.c client.pl testing.txt answers.txt This "cstsubmit" program will copy the selected files to me for marking. Always submit *all* your files at the same time. Do not delete your copies; keep them. Verify that you submitted all your files, using this command line: $ ~alleni/bin/cstsubmit 01 -list Note that the digit "1" and the letter "l" (lower-case "L") are different. Do not confuse the two. You may redo this exercise and re-submit your results as many times as you like; but, you must always submit *all* your exercise files every time. The "-delete" option of cstsubmit will delete the most recent submission you have made. I will mark only the most recent submission that is submitted before the final hand-in cutoff date. For Exercise 01, always use "01" as the first argument to "cstsubmit". Always submit *all* the files each time you submit an exercise. A correct server2.c file is worth 50% of the mark. A correct client.pl file is worth 10% of the mark. Your testing output is worth 10% of the mark. Your answer file is worth 30% of the mark. P.S. Did you spell all the assignment label fields and file names correctly?