Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This needs to implemented in Python using the Socket Library Write a simple TCP-

ID: 3722637 • Letter: T

Question

This needs to implemented in Python using the Socket Library

Write a simple TCP-based client server application where the client sends a text message to the server that computes a digest of the message and sends it back to the client. The client should Connect to the server Read a sequence of characters from the user and sends it to the server. If input is 'exit' go to step 4 Receive the digest from the server, print the digest and port numbers as shown below and go to step 2 Close connection and exit 1. 2. 3. 4. Input sentence: test 1 Sent test1' on port 57286 to server 127.0.0.1,13000 Received digest '529'on port 57286 from server 127.0.0.1 13000 The server should: Wait for a connection from a new client Receive an input string from the client process. If input is equal to 'exit' then go to step 6 Compute a digest by adding the ASCII representation of all the characters in the character string Print the digest and the port numbers as shown below Send the digest back to the client and go back to step 2 Close connection and go back to step 1 1. 2. 3. 4. 5. 6. client (IP: 127.0.0.1 port: 57286sent'test 1 'and we sent back 529

Explanation / Answer

from server side

# Python program to implement server side of chat room.
import socket
import select
import sys
from thread import *

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# checks whether sufficient arguments have been provided
if len(sys.argv) != 3:
print "Correct usage: script, IP address, port number"
exit()

# takes the first argument from command prompt as IP address
IP_address = str(sys.argv[1])

# takes second argument from command prompt as port number
Port = int(sys.argv[2])

"""
binds the server to an entered IP address and at the
specified port number.
The client must be aware of these parameters
"""
server.bind((IP_address, Port))

"""
listens for 100 active connections. This number can be
increased as per convenience.
"""
server.listen(100)

list_of_clients = []

def clientthread(conn, addr):

# sends a message to the client whose user object is conn
conn.send("Welcome to this chatroom!")

while True:
try:
message = conn.recv(2048)
if message:

"""prints the message and address of the
user who just sent the message on the server
terminal"""
print "<" + addr[0] + "> " + message

# Calls broadcast function to send message to all
message_to_send = "<" + addr[0] + "> " + message
broadcast(message_to_send, conn)

else:
"""message may have no content if the connection
is broken, in this case we remove the connection"""
remove(conn)

except:
continue

"""Using the below function, we broadcast the message to all
clients who's object is not the same as the one sending
the message """
def broadcast(message, connection):
for clients in list_of_clients:
if clients!=connection:
try:
clients.send(message)
except:
clients.close()

# if the link is broken, we remove the client
remove(clients)

"""The following function simply removes the object
from the list that was created at the beginning of
the program"""
def remove(connection):
if connection in list_of_clients:
list_of_clients.remove(connection)

while True:

"""Accepts a connection request and stores two parameters,
conn which is a socket object for that user, and addr
which contains the IP address of the client that just
connected"""
conn, addr = server.accept()

"""Maintains a list of clients for ease of broadcasting
a message to all available people in the chatroom"""
list_of_clients.append(conn)

# prints the address of the user that just connected
print addr[0] + " connected"

# creates and individual thread for every user
# that connects
start_new_thread(clientthread,(conn,addr))   

conn.close()
server.close()

client side

# Python program to implement client side of chat room.
import socket
import select
import sys

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:
print "Correct usage: script, IP address, port number"
exit()
IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.connect((IP_address, Port))

while True:

sockets_list = [sys.stdin, server]
read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])

for socks in read_sockets:
if socks == server:
message = socks.recv(2048)
print message
else:
message = sys.stdin.readline()
server.send(message)
sys.stdout.write("<You>")
sys.stdout.write(message)
sys.stdout.flush()
server.close()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote