Skip to document

Lab 4 - ---

---
Course

Networking Fundamentals (41092)

238 Documents
Students shared 238 documents in this course
Academic year: 2021/2022
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
University of Technology Sydney

Comments

Please sign in or register to post comments.

Preview text

Week4 Lab: Socket Programming

Procedure for running the Python UDP and TCP client and server

Setup Project:

You should have installed Python and PyCharm. Note that the Linux and MacOS machines

has existing Python 2 and Windows machines are running python 3, so there are some minor

differences between the programs for the two operating systems. Our sample codes use

Python 3.

1) In PyCharm, create a project, ClientServer

2) Copy and paste the code from the end of this document (Appendix) into four separate files in

the project: TCPServer, UDPServer, TCPClient and UDPClient

3) Find your PC’s IP address

a. in Windows:

i. type “cmd” in the search box on windows and pressing <ENTER>

ii. In the command window type “ipconfig” to find your IP address.

b. in MacOS:

i. type “terminal” in the search box and pressing <ENTER>

ii. in the terminal window, type “ifconfig” to find you IP address

Run your programs

4) run UDP server in PyCharm – screenshot below:
5) Run UDP Client in PyCharm

Add server IP address to the Client file, and run the client file.

The program will prompt you for a string of characters. Enter lower case sentence and

<ENTER>.

If everything has been properly configured you string should be returned in all upper case.

6) For TCP Server and Client: the procedure is the same for setting up and running.
-

(T5) (Optional – if there is another PC available in your network.) Find out the IP addresses

of the machine next to you. Modify your server code so that it can access servers running on

those machines. You could hard code in the server addresses or pass the data as part of the

command line or have the program prompt the user for the IP address.

Appendix. Python Code for Week4 Lab

UDPServer

from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket(('', serverPort)) print("The server is ready to receive") while True: message, clientAddress = serverSocket( 2048 ) modifiedMessage = message().upper() serverSocket(modifiedMessage(), clientAddress)

UDPClient

from socket import * serverName = '192.168.0' serverPort = 12000 clientSocket = socket(AF_INET, SOCK_DGRAM) message = input('Input lowercase sentence:') clientSocket(message(),(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket( 2048 ) print(modifiedMessage()) clientSocket()

TCPServer

from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket(('', serverPort)) serverSocket( 1 ) print("The server is ready to receive") while True: connectionSocket, addr = serverSocket() sentence = connectionSocket( 1024 ).decode() capitalizedSentence = sentence() connectionSocket(capitalizedSentence()) connectionSocket()

TCPClient

from socket import * serverName = '192.168.0' serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket((serverName, serverPort)) sentence = input('Input a lower case sentence : ') clientSocket(sentence()) modifiedSentence = clientSocket( 1024 ) print('From Server : ' + modifiedSentence()) clientSocket() print("complete")

Was this document helpful?

Lab 4 - ---

Course: Networking Fundamentals (41092)

238 Documents
Students shared 238 documents in this course
Was this document helpful?
Week4 Lab: Socket Programming
Procedure for running the Python UDP and TCP client and server
Setup Project:
You should have installed Python and PyCharm. Note that the Linux and MacOS machines
has existing Python 2 and Windows machines are running python 3, so there are some minor
differences between the programs for the two operating systems. Our sample codes use
Python 3.
1) In PyCharm, create a project, ClientServer
2) Copy and paste the code from the end of this document (Appendix) into four separate files in
the project: TCPServer.py, UDPServer.py, TCPClient.py and UDPClient.py
3) Find your PCs IP address
a. in Windows:
i. type “cmd” in the search box on windows and pressing <ENTER>
ii. In the command window type “ipconfig” to find your IP address.
b. in MacOS:
i. type “terminal” in the search box and pressing <ENTER>
ii. in the terminal window, type “ifconfig” to find you IP address