lms.courselearn.net · web viewpython has two standard versions, python 2 and python 3. the current...
TRANSCRIPT
EE502—Computer Communication Networks
Programming Assignment (Week 2)
Simple client-server application using UDP and TCP
ObjectivesThis laboratory exercise is to introduce students to Python socket programming through hands-on experiment. Students will better understand how to use Python GUI to program a simple client server that uses UDP and TCP.
Equipment ListTwo PCs or laptops installed with Python 2.7.9
Python 2 setup
Python is a general purpose, high-level programming language that is used in a variety of application domains. The Python language has a very clear and expressive syntax, as well as a large and comprehensive library. Although Python is often used as a scripting language, it can also be used in a wide range of non-scripting contexts. It’s available for all major operating systems: Windows, Linux/UNIX, OS/2, Mac, Amiga, among others. Python is free to use, even for commercial products, because of its OSI-approved open-source license.
Standard versionsPython has two standard versions, Python 2 and Python 3. The current production versions are Python 2.7.9 and Python 3.4.2. Python 3 is not backward compatible. If you download version 3, you may have to slightly modify the code given in the examples of the recommended textbook (Kurose & Ross, 6th edition) for this course before it can run without errors. Python 2.7.9 is recommended for completing the assignments in this course. It can be downloaded directly from the official website http://www.python.org/. This is the program that is used to write all your Python code.
DownloadClicking the Downloads link will present you with two versions of Python, namely Python 2 and Python 3, as shown in Figure 1 below; select Download Python 2.7.9, you can also chose the version specific to your operating system. Most popular Linux distributions come with Python in the default installation. Mac OSX 10.2 and later includes a command-line version of Python, although you'll probably want to install a version that includes a more Mac-like graphical interface.
1
Figure 1
Installing Python on Windows
1. Click on Run and select whether to install for all users (if any) according to your preference, as shown in Figure 2 and select Next.
Figure 2
2
2. Select destination directory (Python27), and click Next.
Figure 3
3. Step through the installer program by clicking Next, and Finish.
Figure 4
4. If you see the following (Figure 5), which means the installation is complete. Click Finish to close the installer.
Figure 5
3
5. To run the Python graphic user interface on Windows 8, go to Apps, you'll see something like Figure 6. Click IDLE (Python GUI) in the folder Python 2.7, as shown below.
Figure 6
6. You should see the Python Shell window as shown in Figure 7.
Figure 7
7. To create your program, click New File; a new window is opened. The new window that pops out is the editor that will allow you to save and execute your Python programs. You will enter your Python code in this editor window.
Figure 8
4
Figure 9
8. As an exercise, enter the above program (Figure 9) into the editor and save your file as Welcome.py.
Figure 10
9. To execute the program, go to Run in the toolbar and select Run Module.
5
Your Python
Welcom
Figure 11
10. You should see Enter your name: in the Python Shell. Type your name (e.g., chng) and click Enter. You should see the return result as shown in Figure 12.
Figure 12
6
Socket Programming with UDP Introduction
In order to demonstrate a simple client-server socket program that uses UDP and TCP, two PCs or laptops are required. The codes for the client side and the server side that uses UDP are both provided, as shown in Figures 14 and 15. The client first reads a line of characters from its keyboard and sends the data to the server. The server receives the data and converts the characters to uppercase. The server then sends the modified data back to the client. After the client receives the modified data, it displays the line on its monitor. Read Chapter 2.7.1 in your recommended textbook, Computer Networking: A top-down approach by Kurose and Ross for a detail explanation of this program.
Figure 13. Client-Server application using UDP
7
Figure 14. UDPClient.py
Figure 15. UDPServer.py
Procedure
1. With the two computers connected to the Ethernet or wireless LAN, assign one computer as server and the other as client. Enter the command on the server computer to find its host name. >>>import socket >>>socket.gethostname()
You should see it returns with the hostname. Record the hostname: ____________
2. Create a Python program (UDPClient.py) on the client computer, as shown in Figure 14. Remember to replace the ‘hostname’ by the hostname of the server in part 1. **You can also use the IP address of the server.
3. The server port can be arbitrarily chosen.
4. Create a Python program (UDPServer.py) on the server computer, as shown in Figure 15. Run UDPServer.py in the server host. This creates a process in the server that idles until it is contacted by the client.
8
5. Next, execute UDPClient.py in the client computer. This creates a process in the client.
6. To test the application, type a sentence in small letters followed by a carriage return on the client machine and observe the output on the shell.
7. Save and attach a screenshot of your running program on both server- and client-side to your report.
The following is an example of the test results on the server and the client machine.
Figure 16. Running server program
9
Figure 17. Running client program
Questions
1. Suppose you run UDPClient.py before you run UDPServer.py. What happens? Why?
2. What happens if you use different port numbers for the client and server sides?
3. Suppose that in UDPClient.py, after you create the socket, you add the line:
clientSocket.bind((‘ ‘,5432))
Will it become necessary to change UDPServer.py?
What are the port numbers for the sockets in UDPClient and UDPServer?
What were they before making this change?
10
Socket Programming with TCPFor two computers to communicate using TCP protocol, handshaking must first occur between the client and the server before a TCP connection can be established, as shown in Figure 18.
Figure 18. Client-server application using TCP
Figure 19. TCPClient.py
11
Figure 20. TCPServer.py
Procedure
1. Create a Python program (TCPClient.py) as shown in Figure 19 on the client computer and a Python program (TCPServer.py) on the server computer, as shown in Figure 20. Remember to replace the ‘hostname’ by the hostname of the server.
2. To test the application, execute both programs on the two computers.
3. Type a sentence in small letters followed by a carriage return on the client machine and observe the output on the shell.
4. Save and attach a screenshot of your running program on both server- and client-side to your report.
Questions
1. Suppose you run TCPClient.py before you run TCPServer.py. What happens? Why?
2. The UDP server needed only one socket, whereas the TCP server needed two sockets. Why?
3. If the TCP server were to support n simultaneous connections, each from a different client host, how many sockets would the TCP server need?
12
Programming Assignment Report—Due at the end of Week 2
Write a basic report commenting on the above API programming experiment. The report must include a cover page with the lab title, the course number, your student name, and your DeVry DSI number. You must also include a basic introduction that addresses the purpose of this programming lab. Students must also include basic background information on how the experiment was set up. Summarize key findings. What were some of the challenges, if any, that you faced in completing this assignment? How did you overcome those challenges? What possible improvement can you make in this assignment? Answer all the questions.
Attach a screenshot of your running programs on both server and client sides.
13