Python Programming Basic TCP/IP exercise. What they are asking is just to improv
ID: 3557233 • Letter: P
Question
Python Programming Basic TCP/IP exercise. What they are asking is just to improve or add little features to the completed program (This exercise is directly from the book and the script/program is provided by the book). The scripts will be provided below by link this is a school related exercise. Version of python used is 3.X or later. I just need to have the program with these working that are below. If you can help me do 2 of them i'll be grateful and more than that would be astonishing. If you have any inquiries/quesiton please be sure to comment.
Chatclient: https://mega.co.nz/#!9MlzCIxS!Kis0FhYZstgUDmfgxV2OVf55kPDmSHXQ7JuD_hyMcJI
Chatserver: https://mega.co.nz/#!5FFUWIqD!BRiP4127K7I8LxStzuH-W8jBzV1EYOJDBIwf_QODhAs
1) Rewrite chatclient script/program to that explicitly informs user that the connnection has been lost.
2) Rewrite the chat server/client so that each time the user tries to send a private message to a non-exister user, the server prints back "UNKNOWNUSER". (In book this is ex.16.10)
3) When a new user Joins to the chatserver/room the program should print/display the name/s of any or all present users apart from them.
4) Rewrite the cleint software so that it does not display an echo of a message sent by the users. Maybe it means that each time a user tries to send a private message that same message is also sent back to them? summary: stop that from happening?
5) Extend chat room software so that private messsages can be sent simultaneously. Multiple priv message at the same time?
Explanation / Answer
# Tcp Chat Server
import socket, select
#Function to broadcast chat messages to all connected clients
def broadcast_data (sock, message):
#Do not send the message to master socket and the client who has send us the message
for socket in CONNECTION_LIST:
if socket != server_socket and socket != sock :
try :
socket.send(message)
except :
# broken socket connection may be, chat client pressed ctrl+c for example
socket.close()
CONNECTION_LIST.remove(socket)
if __name__ == "__main__":
# List to keep track of socket descriptors
CONNECTION_LIST = []
RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
PORT = 5000
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# this has no effect, why ?
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(("0.0.0.0", PORT))
server_socket.listen(10)
# Add server socket to the list of readable connections
CONNECTION_LIST.append(server_socket)
print "Chat server started on port " + str(PORT)
while 1:
# Get the list sockets which are ready to be read through select
read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
for sock in read_sockets:
#New connection
if sock == server_socket:
# Handle the case in which there is a new connection recieved through server_socket
sockfd, addr = server_socket.accept()
CONNECTION_LIST.append(sockfd)
print "Client (%s, %s) connected" % addr
broadcast_data(sockfd, "[%s:%s] entered room " % addr)
#Some incoming message from a client
else:
# Data recieved from client, process it
try:
#In Windows, sometimes when a TCP program closes abruptly,
# a "Connection reset by peer" exception will be thrown
data = sock.recv(RECV_BUFFER)
if data:
broadcast_data(sock, " " + '<' + str(sock.getpeername()) + '> ' + data)
except:
broadcast_data(sock, "Client (%s, %s) is offline" % addr)
print "Client (%s, %s) is offline" % addr
sock.close()
CONNECTION_LIST.remove(sock)
continue
server_socket.close()
#Chat CLient
import socket, select, string, sys
def prompt() :
sys.stdout.write('<You> ')
sys.stdout.flush()
#main function
if __name__ == "__main__":
if(len(sys.argv) < 3) :
print 'Usage : python telnet.py hostname port'
sys.exit()
host = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
# connect to remote host
try :
s.connect((host, port))
except :
print 'Unable to connect'
sys.exit()
print 'Connected to remote host. Start sending messages'
prompt()
while 1:
socket_list = [sys.stdin, s]
# Get the list sockets which are readable
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
#incoming message from remote server
if sock == s:
data = sock.recv(4096)
if not data :
print ' Disconnected from chat server'
sys.exit()
else :
#print data
sys.stdout.write(data)
prompt()
#user entered a message
else :
msg = sys.stdin.readline()
s.send(msg)
prompt()
Another method-Chat Server
#!/usr/bin/python
from asyncore import dispatcher
from asynchat import async_chat
import socket, asyncore
PORT = 5006
NAME =
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.