diff --git a/server.py b/server.py index fcc2550a07808844f3fdc566ed6d2a387003b2f5..aa2961b54e456792f46802bdbab4002c2f000535 100644 --- a/server.py +++ b/server.py @@ -1,31 +1,56 @@ import base64 import asyncio import websockets -""" -# Connecting to the server -async def receive_message(): - ws = websockets.WebSocket() - await ws.connect("ws://localhost:5612") - - await ws.send("Testing Testing") - response = await ws.recv() - print(f"Server's Respons: {response}") +# Connect to server and receive a message +async def receive_message(uri="ws://localhost:5612"): + async with websockets.connect(uri) as ws: + #ws = websockets.websocket() + + await ws.send("Server Test") + # Response from server + response = await ws.recv() + + # Close connection + await ws.close() - await ws.close() + # Print the Base64 + print("Base64:", response) + + # Decode the base64 message + packet = base64.b64decode(response) + + # Print the decoded packet + print("Server Sent: ", packet) + + # Extract fields from the packet + source_port = int.from_bytes(packet[0:2], "little") + dest_port = int.from_bytes(packet[2:4], "little") + length = int.from_bytes(packet[4:6], "little") + checksum = int.from_bytes(packet[6:8], "little") + payload = packet[8:].decode("utf-8") + # Packet after decoding + print("Packet:") + print(f"Source Port: ", source_port) + print("Dest Port: ", dest_port) + print("Data Length: ", length) + print("Checksum: ", checksum) + print("Payload: ", payload) + # Asyncio event loop asyncio.get_event_loop().run_until_complete(receive_message()) -""" + +""" # Test Function to test connection to server async def test_connection(uri): async with websockets.connect(uri) as websocket: # Send a message await websocket.send("Server Test") - # wait for response response = await websocket.recv() print(response) # Runs test_connection function -asyncio.get_event_loop().run_until_complete(test_connection('ws://localhost:5612')) \ No newline at end of file +asyncio.get_event_loop().run_until_complete(test_connection('ws://localhost:5612')) +""" \ No newline at end of file