Network Architecture socket programming using Python

profilemahesh6wk
NetworkArchitecturesocketprogrammingusingPython.docx

Network Architecture socket programming using Python

Group Chatting Program

Develop a simple chat program (similar to google hangout and skype chat)

This should be the continuation of first Part for which the details are mentioned below.

Extend the first part of program to ‘chat client-server’ programing for following steps (a to d) and share the code. I’ll be running the code in my machine (Client & Server in same machine) and take screenshots.

a) A chat server will accept a single client connection and display everything the client

types. If the client user types ‘exit’, both client and server will end the program.

b) A server now remains ‘open’ for additional connection once a client quits. The

server can handle at most one connection at a time.

c) A server now can handle multiple clients at the same time. The output from all

the connected clients will appear on the server’s screen.

d) A server replies next 3 days temperature of Kansas city ( eg 30 C/45 F), when client sends

“Weather” otherwise server echoes same message.

Ex: Client: Weather (Case-insensitive)

Server: Next consecutive three days temperature : 45c/47/34c (student choice units:c/f)

Client : hello

Server: hi

First Part details: Here is the first program details for which I have written the code (Need to extend the code to group chatting in python):

Part I. Socket programming (No need to develop this part but the code Is given for reference as the second part Is the extension of first part )

Develop simple TCP client and server programs locally, but test with another machine (eg. Another

machine in UMKC network). Show the screenshots of simple message exchanges.

a) (20%) Start from client message ‘Hello from Client-your names’ and server responses

with ‘Hello from Server-your names’. Then messages from each side are echoed to each

other. The program quit the program with typing ‘Bye from Client-your name’ and ‘Bye

from Server-your name’.

Client code for first part :

import socket

host = 'localhost'

port = 8080

def connect(host,port):

serv=socket.socket()

serv.connect((host,port))

return serv

def readinput():

msg=input("Me: ")

return msg

def exchange_messages(m,serv):

while True:

if m == "Hello From Client Garima ":

serv.send(str.encode(m))

message=str(serv.recv(8080).decode())

print("Server:" + message)

m=readinput()

elif m == "Bye From Client Garima":

serv.send(str.encode(m))

message=str(serv.recv(8080).decode())

print("Server: " + message)

if(message == "Bye From Server Garima "):

break

else:

m=readinput()

else:

serv.send(str.encode(m))

message=str(serv.recv(8080).decode())

print("Server:"+ message)

m=readinput()

def main():

serv=connect(host,port)

m=print("Enter message here...")

m=input("Me: ")

exchange_messages(m,serv)

serv.close()

if __name__== "__main__":

main()

Server code for First part:

import socket

host = ''

port = 8080

def bind(host,port):

serv=socket.socket()

serv.bind((host, port))

print('Messages are below')

serv.listen(1)

conn,addr = serv.accept()

return conn

def exchange_messages(conn):

while True:

message=str(conn.recv(8080).decode())

if message == "Hello From Client Garima ":

print("Client:" + message)

conn.send(str.encode("Hello From Server Garima "))

elif message == "Bye From Client Garima ":

print("Client:" + message)

conn.send(str.encode("Bye From Server Garima "))

break

else:

print("Client:"+ message)

reply=input("Me: ")

conn.send(str.encode(reply))

def main():

conn=bind(host, port)

exchange_messages(conn)

conn.close()

if __name__== "__main__":

main()