Question: Forwarding on TCP connection ( 5 0 Points ) Figure 1 . Architecture of Forwarder We will use three python codes - - ( 1
Forwarding on TCP connection Points
Figure Architecture of Forwarder
We will use three python codes TCP server, TCP client, and
forwarder.py a combination of
TCP client and server as illustrated above Utilize the TCP server and client codes designed in project
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. 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
points
TCPClient Code:
import socket
import threading
def receivemessagessock:
while True:
try:
message sock.recvdecodeutf
if message:
printmessage
else:
printDisconnected from server"
break
except:
printAn error occurred. Disconnecting..."
sock.close
break
def sendmessagessock:
while True:
message input
try:
sock.sendmessageencodeutf
if message.lower 'exit':
break
except:
printAn error occurred. Disconnecting..."
sock.close
break
def mainserverip serverport:
sock socket.socketsocketAFINET, socket.SOCKSTREAM
try:
sock.connectserverip serverport
printConnected to server"
# Start the thread for receiving messages
threading.Threadtargetreceivemessages, argssockstart
sendmessagessock
except Exception as e:
printfFailed to connect to server: e
finally:
sock.close
if namemain:
SERVERIP "localhost"
SERVERPORT
mainSERVERIP SERVERPORT
TCPSERVER Code:
from socket import
import threading
import sys
PORT
clients
def broadcastmessagemessage senderid:
for id client in clients.items:
if id senderid:
try:
client.sendmessage
except:
# Removing the client if it's no longer connected
del clientsid
def handlerclientsock addr:
# Prompting new client for an identifier
clientsock.sendbEnter your ID:
id clientsock.recvdecodestrip
clientsid clientsock # Adding the client with its ID
while True:
try:
data clientsock.recv
if not data:
break
sysstderr.writefid sent datadecode
# Broadcasting received message to other clients, including senders ID
message fid: datadecodeencode
broadcastmessagemessage id
if "exit" data.decodestriplower:
break
except ConnectionResetError:
break
# Removing the client on disconnect
del clientsid
clientsock.close
sysstderr.writefClosed connection with id
def startserver:
serveraddress localhost PORT
serversock socketAFINET, SOCKSTREAM
serversock.setsockoptSOLSOCKET, SOREUSEADDR,
serversock.bindserveraddress
sysstderr.writeServer starting up
serversock.listen # Listening for up to clients
while True:
sysstderr.writeWaiting for connection...
clientsock, clientaddress serversock.accept
sysstderr.writefClient connected: clientaddress
x threading.Threadtargethandler, argsclientsock clientaddress,
xstart
startserver
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
