Uploaded by munigalofficial

csc1010 lab9 ws

advertisement
CSC1010 Computer Networks
2021-2022 Trimester 2
Laboratory 9:
Socket Programming
Learning outcomes
Upon completion of this laboratory exercise, you should be able to:
l
Use Wireshark to capture network packets
l
Understand the operations of TCP/IP and the Internet
l
UDP Programming using Python
l
TCP Programming using Python
l
Capture and analyze TCP and UDP packets of the Python code
Topology
Addressing Table
Device
PC-A (Simulated
Interface
IP Address
Subnet Mask
Default Gateway
NIC
192.168.1.10
255.255.255.0
192.168.1.1
NIC
192.168.1.20
255.255.255.0
192.168.1.1
Server)
PC-B
Required hardware
l
1 x Rack of Cisco network devices
l
2 x PC
l
Ethernet cables
CSC1010 Computer Networks
Required software
l
Wireshark 3.2.0
https://www.wireshark.org
l
Python 2.7.16
https://www.python.org/
Part 3: TCP socket programming
Sockets can be configured to act as a server and listen for incoming messages, or
connect to other applications as a client. After both ends of a TCP/IP socket are
connected, communication is bi-directional.
Echo Server
This sample program, based on the one in the standard library documentation,
receives incoming messages and echos them back to the sender. It starts by
creating a TCP/IP socket, then bind() is used to associate the socket with the
server address. In this case, the address is localhost, referring to the current
server, and the port number is 10000. Calling listen() puts the socket into server
mode, and accept() waits for an incoming connection. The integer argument is the
number of connections the system should queue up in the background before
rejecting new clients. This example only expects to work with one connection at a
time.
accept() returns an open connection between the server and client, along with the
address of the client. The connection is actually a different socket on another port
(assigned by the kernel). Data is read from the connection with recv() and
transmitted with sendall().
When communication with a client is finished, the connection needs to be cleaned up
using close(). This example uses a try:finally block to ensure
that close() is always called, even in the event of an error.
CSC1010 Computer Networks
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print('waiting for a connection')
connection, client_address = sock.accept()
try:
print('connection from', client_address)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print('received {!r}'.format(data))
if data:
print('sending data back to the client')
connection.sendall(data)
else:
print('no data from', client_address)
break
finally:
# Clean up the connection
connection.close()
CSC1010 Computer Networks
Echo Client
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print('connecting to {} port {}'.format(*server_address))
sock.connect(server_address)
try:
# Send data
message = b'This is the message. It will be repeated.'
print('sending {!r}'.format(message))
sock.sendall(message)
# Look for the response
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = sock.recv(16)
amount_received += len(data)
print('received {!r}'.format(data))
finally:
print('closing socket')
sock.close()
The client program sets up its socket differently from the way a server does.
Instead of binding to a port and listening, it uses connect() to attach the socket
directly to the remote address.
After the connection is established, data can be sent through
the socket with sendall() and received with recv(), just as in the server. When
the entire message is sent and a copy received, the socket is closed to free up the
port.
CSC1010 Computer Networks
Step 1: Copy the Echo Server code and create a python file “TCP_server.py”
Step 2: Copy the Echo Client code and create a python file “TCP_client.py”
Step 3: Execute Echo Server in your local PC
Step 4: Execute Echo Client in your local PC, you should see the console output as
follow:
Server program after connection
Client Program after connection
Question: How to change the code so that the client will output as follow:
CSC1010 Computer Networks
CSC1010 Computer Networks
Part 4: UDP socket programming
UDP is the abbreviation of User Datagram Protocol. UDP makes use of Internet
Protocol of the TCP/IP suit. In communications using UDP, a client program sends a
message packet to a destination server wherein the destination server also runs on
UDP.
UDP
Server
import
socket
localIP
= "127.0.0.1"
localPort
= 20001
bufferSize = 1024
msgFromServer
= "Hello UDP Client"
bytesToSend
= str.encode(msgFromServer)
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET,
type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = "Message from Client:{}".format(message)
clientIP = "Client IP Address:{}".format(address)
print(clientMsg)
print(clientIP)
# Sending a reply to client
UDPServerSocket.sendto(bytesToSend, address)
CSC1010 Computer Networks
UDP Client
import socket
msgFromClient
= "Hello UDP Server"
bytesToSend
= str.encode(msgFromClient)
serverAddressPort
bufferSize
= ("127.0.0.1", 20001)
= 1024
# Create a UDP socket at client side
UDPClientSocket = socket.socket(family=socket.AF_INET,
type=socket.SOCK_DGRAM)
# Send to server using created UDP socket
UDPClientSocket.sendto(bytesToSend, serverAddressPort)
msgFromServer = UDPClientSocket.recvfrom(bufferSize)
msg = "Message from Server {}".format(msgFromServer[0])
print(msg)
Step 1: Copy the UDP Server code and create a python file “UDP_server.py”
Step 2: Copy the UDP Client code and create a python file “UDP_client.py”
Step 3: Execute UDP Server in your local PC
Step 4: Execute UDP Client in your local PC, you should see the console output as
follow:
Question: What is the difference between TCP and UDP communication?
Server program after connection
Client program after connection
CSC1010 Computer Networks
Reference
https://pythontic.com/modules/socket/udp-client-server-example
https://pymotw.com/3/socket/tcp.html
Download