Question: Forwarding on TCP connection ( 5 0 Points ) Figure 1 . Architecture of Forwarder We will use three python codes - - ( 1

Forwarding on TCP connection (50 Points)
Figure 1. Architecture of Forwarder
We will use three python codes --(1) TCP server, (2) TCP client, and (3)
forwarder.py (a combination of
TCP client and server as illustrated above). Utilize the TCP server and client codes designed in project 3.
Design "
forwarder.py" as the following:
A TCP server must start first and wait for a forwarder. The forwarder contains TCP server and clients.
A TCP client in the forwarder will establish a connection to the TCP server. For this connection, you
must use a TCP client in the forwarder. (25 Points)
Once your forwarder is connected to the TCP server, the TCP server in the forwarder will start and
then, a TCP client must start. If you type a word on the TCP client, the TCP client will send this word
to the forwarder. The forwarder will forward this word to the TCP server as shown in Figure 1(25
points)
TCP_Client Code:
import socket
import threading
def receive_messages(sock):
while True:
try:
message = sock.recv(1024).decode('utf-8')
if message:
print(message)
else:
print("Disconnected from server")
break
except:
print("An error occurred. Disconnecting...")
sock.close()
break
def send_messages(sock):
while True:
message = input('')
try:
sock.send(message.encode('utf-8'))
if message.lower()== 'exit':
break
except:
print("An error occurred. Disconnecting...")
sock.close()
break
def main(server_ip, server_port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((server_ip, server_port))
print("Connected to server")
# Start the thread for receiving messages
threading.Thread(target=receive_messages, args=(sock,)).start()
send_messages(sock)
except Exception as e:
print(f"Failed to connect to server: {e}")
finally:
sock.close()
if __name__=="__main__":
SERVER_IP = "localhost"
SERVER_PORT =10000
main(SERVER_IP, SERVER_PORT)
TCP_SERVER Code:
from socket import *
import threading
import sys
PORT =10000
clients ={}
def broadcast_message(message, sender_id):
for id, client in clients.items():
if id != sender_id:
try:
client.send(message)
except:
# Removing the client if it's no longer connected
del clients[id]
def handler(clientsock, addr):
# Prompting new client for an identifier
clientsock.send(b'Enter your ID:')
id = clientsock.recv(1024).decode().strip()
clients[id]= clientsock # Adding the client with its ID
while True:
try:
data = clientsock.recv(1024)
if not data:
break
sys.stderr.write(f'{id} sent "{data.decode()}"
')
# Broadcasting received message to other clients, including sender's ID
message = f"{id}: {data.decode()}".encode()
broadcast_message(message, id)
if "exit" == data.decode().strip().lower():
break
except ConnectionResetError:
break
# Removing the client on disconnect
del clients[id]
clientsock.close()
sys.stderr.write(f"Closed connection with {id}
")
def start_server():
server_address =('localhost', PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serversock.bind(server_address)
sys.stderr.write('Server starting up
')
serversock.listen(5) # Listening for up to 5 clients
while True:
sys.stderr.write('Waiting for connection...
')
clientsock, client_address = serversock.accept()
sys.stderr.write(f'Client connected: {client_address}
')
x = threading.Thread(target=handler, args=(clientsock, client_address,))
x.start()
start_server()
Forwarding on TCP connection ( 5 0 Points )

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!