Question
PLEASE HELP ME DEBUG PYTHON SOCKET!!! Server is showing struct.error: unpack requires a buffer of 20 bytes This is my EchoClient code: This is my
PLEASE HELP ME DEBUG PYTHON SOCKET!!! Server is showing "struct.error: unpack requires a buffer of 20 bytes"
This is my EchoClient code:
This is my EchoServer code: With the above two codes, I'm able to satisfy all the following requirements but the Server code will show "struct.error: unpack requires a buffer of 20 bytes". Requirements are here: - The echo server runs at TCP port 8899 - The echo server can serve more than one client at a time - The echo server uses a non-blocking mode with multi-thread approach - The echo client and server both use the same message format like this
- Type has two values: 8 and 0. 8 stands for client request and 0 stands for server reply. - Code is always all zeros. - Unused is a field with all ones. - Identifier is a random number (16-bit) generated by the echo client. When the echo server replies, the replied message should carry the exact same identifier as the corresponding request. - There can be more than one request at a time sent by the client, so you can use the Sequence Number to distinguish different requests from the same client. A sequence number always starts from 1. When the echo server replies, the replied message should carry the same sequence number as the corresponding request. For example, a client can send three requests to the server by using one TCP connection. In this case, the client may send three request message with id=1234, seq = 1, 2 and 3 within one TCP session. - Message is a 32-bit field that carrying strings that ends with 0x00. Please write your number (encoded by ASCII) in this field.
- When the server receives an echo request, it should send an echo reply back to the client with the same id, the same seq and the same message. However the Type should be 0. - After receiving the reply, the client will close the TCP connection. - But the server can continually serve more clients.
1 import socket 2 import struct 3 import threading 4. import binascii 5 import random 6 7 s = socket.socket (socket.AF_INET, socket. SOCK_STREAM) 8. 9 pars = ('127.0.0.1', 8899) # server IP and server port 10 s.connect(pars) 11 12 seq=1 13 v while True: 14 Type = 8 15 Code = 0 16 Unused = 65535 17 Identifier = random.randint(1,32767) #use random to produce Identifier 18 msg = "129643879". encode("ascii") 19 senddata = struct.pack('!bbHHH9sxxx', Type, Code, Unused, Identifier, seq, msg) 20 # let the client talk firt 21 s.send( senddata) 22 23 # then wait for server response 24 data = s.recv(1024) 25 v if data: 26 print("data from server:", data) 27 seq+=1 28 v if seq==4: 29 seq=1 30 31 # terminate s.send(b'close') break 33 34 35 36 # close directly s.close() 1 import socket 2 import threading #import threading: multi-thread 3 import struct 4 5 S = socket.socket (socket. AF_INET, socket. SOCK_STREAM #socket, tcp connection 6 S.setsockopt (socket. SOL_SOCKET, socket.SO_REUSEADDR, 1) 7 pars = ('127.0.0.1', 8899) #set up the ip and the port 8 s.bind(pars) 9 s.listen (5) 10 print("Now listening on 127.0.0.1:8899") 11 12 v def serverClient(clientsocket, address): 13 # We need a loop to continuously receive messages from the client 14 v while True: 15 data = clientsocket.recv(1024) 16 v if data: 17 client = struct.unpack('!bbHHH9sxxx', data) 18 print(client) 19 Type = 0 20 Code = client [1] 21 Unused = client [2] 22 Identifier = client [3] 23 seq = client [4] 24 msg = client [5] 25 dsend = struct.pack('!bbHHH9sxxx', Type, Code, Unused, Identifier, seq, msg) 26 # then receive at most 1024 bytes message and store these bytes in a variable named 'data' 27 # you can set the buffer size to any value you like 28 clientsocket.send(dsend) 29 30 v if data == b'close': 31 clientsocket.close() 32 break 33 v while True: 34 # accept a new client and get it's information 35 (clientsocket, address) = s.accept() 36 37 # create a new thread to serve this new client 38 # after the thread is created, it will start to execute 'target' function with arguments 'args' 39 threading. Thread(target = serverClient, args = (clientsocket, address)).start() 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Type | Code | Unused | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identifier | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Message | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started