Question: Question: Use Python to write two socket programs: one for echo server and another for echo client. Must follow all requirements: - The echo server
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 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.
My Client code is done here:
And now I need help with my server code!!! (I'm trying to edit it with my previous code): 
0 1 N 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 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 1 Identifier | Sequence Number 1 +-+-+-+-+-+ --+-+-+-+-+-+-+-+-+-+-+-+ --+-+-+- --+-+-+-+ | Message | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ H: 16bit b:8 bit import socket import struct import threading import binascii import random s = socket.socket (socket.AF_INET, socket. SOCK_STREAM) pars = ('127.0.0.1', 8899) s.connect(pars) Identifier = random.randint(1,32767) #use random to produce Identifier seq=1 while True: data = struct.pack('!bbHHH9sxxx', 8, 0, 65535, Identifier, seq, "129657089".encode("ascii")) # let the client talk firt s.send(b'request') # then wait for server response data = s.recv(1024) if data: print("data from server:", data) seq+=1 if seq==4: seq=1 # terminate s.send(b'close') break # close directly s.close() import socket import threading import struct s = socket.socket (socket.AF_INET, socket. SOCK_STREAM) s.setsockopt (socket. SOL_SOCKET, socket.so_REUSEADDR, 1) pars = ('127.0.0.1', 8899) s.bind(pars) s.listen (5) print("Now listening on 127.0.0.1:8899") def serverclient (clientsocket, address): # we need a loop to continuously receive messages from the client while True: data = struct.pack('!bbHHH9 sxxx', 8, 0, 65535, 1234, seg, "129657089".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()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
