From 9bdefc354b16ff7e84fd58e63d7347f0e54fd0fc Mon Sep 17 00:00:00 2001
From: j2-caldwell <james2.caldwell@live.uwe.ac.uk>
Date: Sat, 22 Jul 2023 09:29:12 +0100
Subject: [PATCH] Successful extraction of ports / fields from packet

---
 server.py | 51 ++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 38 insertions(+), 13 deletions(-)

diff --git a/server.py b/server.py
index fcc2550..aa2961b 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
-- 
GitLab