Question: Use Python to write two socket programs: one for echo server and another for echo client. Must follow all requirements: - The echo server runs
Use Python to write two socket programs: one for echo server and another for echo client.
Must follow all requirements: - 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. I have done the echoClient code but need help with echoServer's. Here's my server code. I am trying to edit it using my previous code:
import socket import threading
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) pars = ('127.0.0.1', 8899) #set up the ip and the port s.bind(pars) s.listen(5) print("Now listening on 127.0.0.1:8899")
def serverClient(clientsocket, address):
while True: data = struct.pack('!bbHHH9sxxx', 8, 0, 65535, 1234, seq, "129708682".encode("ascii")) data = clientsocket.recv(1024) print("from client", data) client_id = int(struct.unpack('!H', [4:6], [0])) client_seq = int(struct.unpack('!H', [6:8], [0]))
if data: clientsocket.send(b'response')
if data == b'close':
clientsocket.close()
break
while True:
(clientsocket, address) = s.accept()
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
Get step-by-step solutions from verified subject matter experts
