Your server will start in a "passive mode", in other words, it will listen to a specified port for instructions from the client. Separately, the client will be started and will connect to the server on a given host name or IP address plus a port number. The client should take as input from the user the host name or IP address plus the port number and an expression to be calculated. Consider solving an expression that would generate relatively big result, allowing you to test transmission of multiple packets.
The client, when started, should request the following information:
The client should make sure that the specified port is valid, in other words, the client should check if the port is in the range from 0 to 65535 and if not should display a message of the format "Invalid port number. Terminating."
The client, then, will try to connect to the specified server on the specified port and transmit the expression to the server.
The interaction flow between the client and server is as follows:
We've seen the overview of socket API and how the client and server can communicate let's create our first client and server.
The server will be echo whatever it recieves.
Echo Server
echo-server.py:
Python
#!/usr/bin/env python3
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
Get Answers For Free
Most questions answered within 1 hours.