Professional Documents
Culture Documents
Pa3 Faria Harrison Hinds Kolp
Pa3 Faria Harrison Hinds Kolp
Programming Assignment 3
Server.py pseudocode
Import the modules from socket
Import the modules from threading
create the serverPort and TCP socket
assign the IP address and port number to TCP socket
Set the server to listen for any incoming connections
Create a function that prints a message to indicate which client connected and store into
clients list
begin a new message thread to manage conversation
create a function that retrieves a message from client, create a check that a message was
inputted, if not then print error message otherwise send message to opposing client on the
same TCP connection
create a function that can be called to have the server send an acknowledgement to the
original sending client
print acknowledgement out
terminate server
David Harrison -
Client.py
Import socket
Establish server vars (IP address and Port)
Create client connection to server
Create msg_in socket
Create msg_out socket
While (!Conversation_End) {
Send input to server
Read output from server
}
Close socket
Server.py
Import socket
Import threading
Create listening port
Create listening socket
Set server to listen
If (message received)
if (messageThread(origin, destination) exists)
sendMessage(origin, destination, message)
Else
messageThread(origin, destination)
sendMessage(origin, destination, message)
Nicholas Hinds -
server.py
start a thread to run the first function that accepts the connections
client.py
Jasper Kolp -
Client.py
Import select, sys, and * from socket modules
Setup connection to the server
serverName = 10.0.0.1, port = 1100
Open TCP socket : socket(AF_INET, SOCK_STREAM)
Prepare for sending and receiving messages
clientSocket.close()
Server.py
Import the modules from socket, threading
create the serverPort and TCP socket
assign the IP address and port number to TCP socket
Set the server to listen for incoming connections
Prepare accepting the clients connection then add clients to start the tread and send
messages
Prepare function for receiving message from one of the clients and send it to other
Create function for sending message. Format message depending on which IP address the
message came from.
2. Basis of decision on best approach (pseudo code) - The group chose to use Nick’s
approach.
5. Test plan - We all tested the client and server files in our own virtual hosts to make sure
the code was compliant on each of our local hosts.
- X first establishes TCP connects with server, X sends first message
- X first establishes TCP connects with server, Y sends first message
- Y first establishes TCP connects with server, X sends first message
- Y first establishes TCP connects with server, Y sends first message e
Server.py
#server_threaded.py
#a chat server using multithreading to allow multiple clients
#tested in mininet with three hosts
from socket import *
from threading import *
serverPort = 1100
# Create a TCP socket
# Notice the use of SOCK_STREAM for TCP packets
serverSocket = socket(AF_INET,SOCK_STREAM)
# Assign IP address and port number to socket
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
# list of clients in the chat server
clients = []
print ('The server is ready to receive')
# accepts the clients connection, adds it clients and starts the thread to send messages
def clientMessages():
while True:
connectionSocket, addr = serverSocket.accept()
clients.append(connectionSocket)
# Establishes which client is Alice and Bob
if addr[0] == '10.0.0.2':
print(addr[0] + " connected as Client X: Alice")
hostIP = addr[0]
elif addr[0] == '10.0.0.3':
print(addr[0] + " connected as Client Y: Bob")
hostIP = addr[0]
# receives message from one of the clients and send it to the other
def send_message(con, hostIP):
while True:
try:
sentence = con.recv(1024)
if sentence:
sent(con, hostIP, sentence)
except Exception as x:
print(x)
break
# sends message
def sent(con, hostIP, message):
for client in clients:
if client != con:
# formats the message based on the IP Address of the client
if hostIP == '10.0.0.3':
orderY = "Y: Bob received before X: Alice"
print(orderY)
clientY = message.decode()
clientY = "ClientY: "+clientY
print(clientY)
clientSentY = orderY+"\n"+clientY
client.send(clientSentY.encode())
else:
orderX = "X: Alice received before Y: Bob"
print(orderX)
clientX = message.decode()
clientX = "ClientX: "+clientX
print(clientX)
clientSentX = orderX+"\n"+clientX
client.send(clientSentX.encode())
Client.py
#client.py
#connects to the server and sends and receives messages to the server
#tested in mininet with three hosts
import select
import sys
from socket import *
# connects to server
serverName = '10.0.0.1'
serverPort = 1100
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
print("Start your conversation")
while True:
# establishes seperate sockects for reading and writing messages from the server
sockets = [sys.stdin, clientSocket]
read,write,error = select.select(sockets,[], [])
clientSocket.close()