Question: Instructions Please read the entire instructions and the skeleton code provided for the server and the client before you start coding. Setup Create two separate
Instructions
Please read the entire instructions and the skeleton code provided for the server and the client before you start coding.
Setup
Create two separate Python files for the server and client, named server.py and client.py respectively.
Copy and paste the provided skeleton code into the respective files.
Implementation
Server: Complete the server code by implementing the unpackpacket function. The server should accept connections, receive custom packets from clients, unpack the packets, and echo back the packet fields.
Client: Complete the client code by implementing the createpacket and handlepacket functions. The client should connect to the server, create packets based on user input and commandline arguments, and display the servers echoed responses.
Testing and Submission
Test your server and client by running them in separate VMs Ensure that they can handle different types of payloads based on the headers service type.
Error Handling: Perform error handling, add comments for clarity, and optimize the code as needed.
Submit your completed server.py and client.py files for evaluation through the submission platform.
Server Skeleton Code
import socket
import struct
def unpackpacketconn headerformat:
# TODO: Implement header unpacking based on received bytes
# TODO: Create a string from the header fields
# return the string this will be the payload
return packetheaderasstring
if namemain:
host 'localhost'
port
# Fixed length header Version byte Header Length byte Service Type byte Payload Length bytes
headerformat # TODO: Specify the header format using "struct"
with socket.socketsocketAFINET, socket.SOCKSTREAM as s:
sbindhost port
slisten
conn, addr saccept
with conn:
printfConnected by: addr
while True:
try:
# TODO: Receive and unpack packet using the unpackpacket function
payloadstring unpackpacketconn headerformat
pass
except:
printConnection closed or an error occurred"
break
#TODO: create header
#TODO: add payload
#TODO: send to client
Client Skeleton Code
import argparse
import socket
import struct
def createpacketversion headerlength, servicetype, payload:
# TODO: Implement packet creation based on parameters
# TODO: use the python struct module to create a fixed length header
# TODO: Fixed length header Version byte Header Length byte Service Type byte Payload Length bytes
# TODO: payload variable length
# TODO: depending on the service type, handle encoding of the different types of payload.
# TODO: servicetype payload is int, servicetype payload is float, servicetype payload is string
return packet
if namemain:
parser argparse.ArgumentParserdescription"Client for packet creation and sending."
parser.addargumentversion', typeint, requiredTrue, help'Packet version'
parser.addargumentheaderlength', typeint, requiredTrue, help'Length of the packet header'
parser.addargumentservicetype', typeint, requiredTrue, help'Service type of the payload for int, for float, for string
parser.addargumentpayload', typestr requiredTrue, help'Payload to be packed into the packet'
parser.addargumenthost', typestr default'localhost', help'Server host'
parser.addargumentport', typeint, default help'Server port'
args parser.parseargs
# TODO: Create and send packet using the createpacket function
packet createpacketargsversion, args.headerlength, args.servicetype, args.payload
#TODO: connect to the server
#TODO: send the packet
#TODO: recive the packet
#TODO: prints header
#TODO: prints payload
Assignment Rubric
Client Implementation points
Client Runs points: The client code runs without errors.
Client Packs the Packet appropriately points: The client can pack a packet.
Type handling: The client code handles different service types properly. The client should be encode different types of payloads int float, string based on the headers service type.
Payload Handling points: Prints the header and the new payload returned by the server. The new payload is client header payload. The header is the header sent by the sever.
Server Implementation points
Server Runs points: The server code runs without errors.
Header handling points: The server reads the header first, followed by the payload indicated by the header.
Decodes payloads properl
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
