Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CST 311 Computer Networking

Professor Harold Millan and Michael Yee


(Team 1) Dalia Faria, David Harrison, Nicholas Hinds, Jasper Kolp
17 February 2020

Programming Assignment 3

1. Team Member Roles


Team Leader - David Harrison
Network & Testing - Dalia Faria, Jasper Kolp
Client & Server Side Programmer - Nicholas Hinds

2. Pseudo code from each member


Dalia Faria​ -
Client.py pseudocode
Import the modules from socket
Create the connection to the server

When a connection is established,


allow messages to be sent
set and print a variable that will display the messages received from server

close the connection

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 list to store all client IDs

Create a thread to accept client connections and start the thread

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 to send message to other connected client


ensure there is a connection, if no connection then print out error message
otherwise send message to opposing client

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)

Def messageThread(origin, destination): #origin = originating IP, destination = destination IP


Identify client by IP
Start thread_send()

Def sendMessage(origin, destination, message)


Send “origin: “ + message to destination
terminate

Nicholas Hinds​ -
server.py

import socket and threading


set the serverPort
create TCP Socket
bind the IP address and port number to the socket
create an array for the clients that connect to the server

define a function that accepts the clients connection


determine which is connected to the server
start thread with the sending function

define a function that receives the message from the client


checks the message and uses another function to send it to the other client.

define a function that determines which client the message is from


then formats the message and sends it to the other client

start a thread to run the first function that accepts the connections

client.py

import select, sys, and socket


set the serverName "IP Address" and serverPort
create the socket and connect to the server
create a while loop

check for messages to recieve


send messages from this client to the server

close the socket

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.

Create thread for accepting client connections

2.​ ​Basis of decision on best approach (pseudo code)​ - The group chose to use Nick’s
approach.

3. Network steps needed (example addresses, sockets)​ - Since this is a TCP


client-server, it is important to set the socket to SOCKET_STREAM. Addresses needed are
host 1 (10.0.0.1), host 2 (10.0.0.2), and host 3 (10.0.0.3) simulated in the mininet virtual
machine. The Server file should be run in host 1. Host 2 and 3 are required to both run the
client file.
4.​ ​Interesting findings from code review discussion ​- We discovered that we did not
necessarily need to incorporate a thread lock for our program’s intended purpose. After
some discussion among the cohort, we realized that the lock would have only helped us
print the messages nicely.

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

6.​ ​Test runs and outputs ​-


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


7. Show the final code

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]

thread_send = Thread(target=send_message, args=[connectionSocket, hostIP])


thread_send.start()

# 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())

# thread for accepting client connections


thread_client = Thread(target = clientMessages)
thread_client.start()

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,[], [])

for soc in read:


if soc == clientSocket:
otherClient = clientSocket.recv(1024)
print (otherClient.decode())
else:
sentence = sys.stdin.readline()
clientSocket.send(sentence.encode())

clientSocket.close()

You might also like