From efd4303818872e60d761f95c3c180e6597fc8b20 Mon Sep 17 00:00:00 2001 From: mtaylor76 Date: Wed, 22 May 2019 22:04:54 -0400 Subject: [PATCH 1/4] adding batch files to run servers as well --- TCP/runServer.bat | 3 +++ UDP/runServer.bat | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 TCP/runServer.bat create mode 100644 UDP/runServer.bat diff --git a/TCP/runServer.bat b/TCP/runServer.bat new file mode 100644 index 0000000..768359c --- /dev/null +++ b/TCP/runServer.bat @@ -0,0 +1,3 @@ +@echo off +python TCPServer.py +pause \ No newline at end of file diff --git a/UDP/runServer.bat b/UDP/runServer.bat new file mode 100644 index 0000000..aaf6ee7 --- /dev/null +++ b/UDP/runServer.bat @@ -0,0 +1,3 @@ +@echo off +python UDPServer.py +pause \ No newline at end of file From 50c28b34ccbce3f3c111bbd323765ea3fb9788ec Mon Sep 17 00:00:00 2001 From: mtaylor76 Date: Thu, 23 May 2019 20:24:41 -0400 Subject: [PATCH 2/4] TCP chat room *kinda* works now? --- TCP/TCPClient.py | 48 +++++++++++++++----- TCP/TCPServer.py | 112 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 126 insertions(+), 34 deletions(-) diff --git a/TCP/TCPClient.py b/TCP/TCPClient.py index 3fbe022..7c1b7c8 100644 --- a/TCP/TCPClient.py +++ b/TCP/TCPClient.py @@ -1,12 +1,40 @@ -#TCPClient.py +# TCPClient.py from socket import socket, AF_INET, SOCK_STREAM -serverName = 'localhost' -serverPort = 12001 -clientSocket = socket(AF_INET, SOCK_STREAM) -clientSocket.connect((serverName, serverPort)) -message = raw_input('Input lowercase sentence: ') -clientSocket.send(message) -modifiedMessage = clientSocket.recv(2048) -print 'From Server: ', modifiedMessage -clientSocket.close() +import select +import sys + +# Initialize server info and create connection +server_name = 'localhost' +server_port = 12001 +server_socket = socket(AF_INET, SOCK_STREAM) +server_socket.connect((server_name, server_port)) + + +print "Interrupt client with CTRL-C" + +while True: + try: + sockets_list = [server_socket] + + read_sockets, write_socket, error_socket = select.select(sockets_list, [], []) + + # We need to append stdin separately in the iteration, since + # Windows can't use select to select a file object + for sockets in read_sockets+[sys.stdin]: + if sockets == server_socket: + message = sockets.recv(2048) + print message + else: + message = sys.stdin.readline() + server_socket.send(message) + sys.stdout.write(" ") + sys.stdout.write(message) + sys.stdout.flush() + + except KeyboardInterrupt: + print "\nInterrupted by CTRL-C" + break + +# If we get here, the connection's done, so make sure to close it. +server_socket.close() diff --git a/TCP/TCPServer.py b/TCP/TCPServer.py index 1369487..068deaf 100644 --- a/TCP/TCPServer.py +++ b/TCP/TCPServer.py @@ -1,28 +1,92 @@ -#TCPServer.py - -#TCP (SOCK_STREAM) is a connection-based protocol. The connection is established and the two -#parties have a conversation until the connection is terminated by one of the parties or by a network error. -from socket import socket, SOCK_STREAM, AF_INET -#Create a TCP socket -#Notice the use of SOCK_STREAM for TCP packets -serverSocket = socket(AF_INET, SOCK_STREAM) -serverPort=12001 +# TCPServer.py + +# TCP (SOCK_STREAM) is a connection-based protocol. The connection is established and the two +# parties have a conversation until the connection is terminated by one of the parties or by a network error. +from socket import socket, SOCK_STREAM, AF_INET, SOL_SOCKET, SO_REUSEADDR +import select +from thread import * + +# Create a TCP socket +# Notice the use of SOCK_STREAM for TCP packets +# Every time you use camelCase to name variables in python, a developer cries +server_socket = socket(AF_INET, SOCK_STREAM) +# Set options here to reuse addresses and make this server suitable for a chat room +server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) + +# Set server port +server_port=12001 + # Assign IP address and port number to socket -serverSocket.bind(('', serverPort)) -serverSocket.listen(1) -print "Interrupt with CTRL-C" +server_socket.bind(('', server_port)) +# Set the chat room to 100 people max +server_socket.listen(100) + +clients_list = [] + + +def clientthread(client, addr): + + # Sent MOTD to the user + client.send("Welcome to the chat! Remember to drink 8 cups of water a day.") + + while True: + try: + # Receive the client packet + message = client.recv(2048) + if message: + print "<" + addr[0] + "> " + message + + # Broadcast message to others in chat room + message_with_address = "<" + addr[0] + "> " + message + broadcast(message_with_address, client) + else: + # If the message is empty or null then something's wrong with the connection, + # so terminate it. + remove(client) + except: + continue + +# This function broadcasts the message to all other connected clients. +def broadcast(message, sender): + for client in clients_list: + # Make sure the message doesn't get repeated to the sender + if client != sender: + try: + client.send(message) + except: + # If there's an exception here, close the troublesome connection + client.close() + remove(client) + +# Utility function to remove a client from the clients list +def remove(client): + if client in clients_list: + clients_list.remove(client) + + +print "Interrupt server with CTRL-C" + while True: try: - connectionSocket, addr = serverSocket.accept() - print "Connection from %s port %s" % addr - # Receive the client packet - message = connectionSocket.recv(2048) - #print "Orignal message from client: ", message - # Capitalize the message from the client - message = message.upper() - connectionSocket.send(message) - connectionSocket.close() + # Accepts the connection, storing the client and address informations + # in their respective variables + client, addr = server_socket.accept() + + # Add client to client list for chat room + clients_list.append(client) + + # Print the address of the new connection + print addr[0] + " has joined the chat." + + # Create new thread for each user that connects + start_new_thread(clientthread, (client, addr)) + except KeyboardInterrupt: - print "\nInterrupted by CTRL-C" - break -serverSocket.close() \ No newline at end of file + print "\nInterrupted by CTRL-C" + break + +# When we get here, the server's done, so close it +client.close() +server_socket.close() + + From cd31dfb9f0026e3dc38644e91aef344b519939d1 Mon Sep 17 00:00:00 2001 From: Matt Taylor Date: Sat, 25 May 2019 21:06:14 -0400 Subject: [PATCH 3/4] revert to older version, misunderstood assignment --- TCP/TCPClient.py | 48 +++++--------------- TCP/TCPServer.py | 112 ++++++++++------------------------------------- 2 files changed, 34 insertions(+), 126 deletions(-) diff --git a/TCP/TCPClient.py b/TCP/TCPClient.py index 7c1b7c8..3fbe022 100644 --- a/TCP/TCPClient.py +++ b/TCP/TCPClient.py @@ -1,40 +1,12 @@ -# TCPClient.py +#TCPClient.py from socket import socket, AF_INET, SOCK_STREAM -import select -import sys - -# Initialize server info and create connection -server_name = 'localhost' -server_port = 12001 -server_socket = socket(AF_INET, SOCK_STREAM) -server_socket.connect((server_name, server_port)) - - -print "Interrupt client with CTRL-C" - -while True: - try: - sockets_list = [server_socket] - - read_sockets, write_socket, error_socket = select.select(sockets_list, [], []) - - # We need to append stdin separately in the iteration, since - # Windows can't use select to select a file object - for sockets in read_sockets+[sys.stdin]: - if sockets == server_socket: - message = sockets.recv(2048) - print message - else: - message = sys.stdin.readline() - server_socket.send(message) - sys.stdout.write(" ") - sys.stdout.write(message) - sys.stdout.flush() - - except KeyboardInterrupt: - print "\nInterrupted by CTRL-C" - break - -# If we get here, the connection's done, so make sure to close it. -server_socket.close() +serverName = 'localhost' +serverPort = 12001 +clientSocket = socket(AF_INET, SOCK_STREAM) +clientSocket.connect((serverName, serverPort)) +message = raw_input('Input lowercase sentence: ') +clientSocket.send(message) +modifiedMessage = clientSocket.recv(2048) +print 'From Server: ', modifiedMessage +clientSocket.close() diff --git a/TCP/TCPServer.py b/TCP/TCPServer.py index 068deaf..1369487 100644 --- a/TCP/TCPServer.py +++ b/TCP/TCPServer.py @@ -1,92 +1,28 @@ -# TCPServer.py - -# TCP (SOCK_STREAM) is a connection-based protocol. The connection is established and the two -# parties have a conversation until the connection is terminated by one of the parties or by a network error. -from socket import socket, SOCK_STREAM, AF_INET, SOL_SOCKET, SO_REUSEADDR -import select -from thread import * - -# Create a TCP socket -# Notice the use of SOCK_STREAM for TCP packets -# Every time you use camelCase to name variables in python, a developer cries -server_socket = socket(AF_INET, SOCK_STREAM) -# Set options here to reuse addresses and make this server suitable for a chat room -server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) - -# Set server port -server_port=12001 - +#TCPServer.py + +#TCP (SOCK_STREAM) is a connection-based protocol. The connection is established and the two +#parties have a conversation until the connection is terminated by one of the parties or by a network error. +from socket import socket, SOCK_STREAM, AF_INET +#Create a TCP socket +#Notice the use of SOCK_STREAM for TCP packets +serverSocket = socket(AF_INET, SOCK_STREAM) +serverPort=12001 # Assign IP address and port number to socket -server_socket.bind(('', server_port)) -# Set the chat room to 100 people max -server_socket.listen(100) - -clients_list = [] - - -def clientthread(client, addr): - - # Sent MOTD to the user - client.send("Welcome to the chat! Remember to drink 8 cups of water a day.") - - while True: - try: - # Receive the client packet - message = client.recv(2048) - if message: - print "<" + addr[0] + "> " + message - - # Broadcast message to others in chat room - message_with_address = "<" + addr[0] + "> " + message - broadcast(message_with_address, client) - else: - # If the message is empty or null then something's wrong with the connection, - # so terminate it. - remove(client) - except: - continue - -# This function broadcasts the message to all other connected clients. -def broadcast(message, sender): - for client in clients_list: - # Make sure the message doesn't get repeated to the sender - if client != sender: - try: - client.send(message) - except: - # If there's an exception here, close the troublesome connection - client.close() - remove(client) - -# Utility function to remove a client from the clients list -def remove(client): - if client in clients_list: - clients_list.remove(client) - - -print "Interrupt server with CTRL-C" - +serverSocket.bind(('', serverPort)) +serverSocket.listen(1) +print "Interrupt with CTRL-C" while True: try: - # Accepts the connection, storing the client and address informations - # in their respective variables - client, addr = server_socket.accept() - - # Add client to client list for chat room - clients_list.append(client) - - # Print the address of the new connection - print addr[0] + " has joined the chat." - - # Create new thread for each user that connects - start_new_thread(clientthread, (client, addr)) - + connectionSocket, addr = serverSocket.accept() + print "Connection from %s port %s" % addr + # Receive the client packet + message = connectionSocket.recv(2048) + #print "Orignal message from client: ", message + # Capitalize the message from the client + message = message.upper() + connectionSocket.send(message) + connectionSocket.close() except KeyboardInterrupt: - print "\nInterrupted by CTRL-C" - break - -# When we get here, the server's done, so close it -client.close() -server_socket.close() - - + print "\nInterrupted by CTRL-C" + break +serverSocket.close() \ No newline at end of file From 81dd11685fedf9b8e4a24e506933af36df187981 Mon Sep 17 00:00:00 2001 From: Matt Taylor Date: Sat, 25 May 2019 21:49:02 -0400 Subject: [PATCH 4/4] solution to UDP program --- .vscode/settings.json | 3 +++ UDP/UDPClient.py | 21 ++++++++++++++++----- UDP/UDPServer.py | 13 +++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..26df38b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.linting.enabled": false +} \ No newline at end of file diff --git a/UDP/UDPClient.py b/UDP/UDPClient.py index 3e7c02f..c0ea252 100644 --- a/UDP/UDPClient.py +++ b/UDP/UDPClient.py @@ -1,14 +1,25 @@ #UDPClient.py -from socket import socket, SOCK_DGRAM, AF_INET +from socket import socket, timeout, SOCK_DGRAM, AF_INET serverName = 'localhost' serverPort = 12000 clientSocket = socket(AF_INET, SOCK_DGRAM) -message = raw_input('Input lowercase sentence: ') -clientSocket.sendto(message, (serverName, serverPort)) -modifiedMessage, addr = clientSocket.recvfrom(2048) -print modifiedMessage, addr +# Set timeout duration to 1 second for clientSocket +clientSocket.settimeout(1) +print "Interrupt with CTRL+C" +while True: + try: + message = raw_input('Input lowercase sentence: ') + clientSocket.sendto(message, (serverName, serverPort)) + modifiedMessage, addr = clientSocket.recvfrom(2048) + print modifiedMessage, addr + # Catch potential timeout + except timeout: + print "Socket timed out after 1 second" + except KeyboardInterrupt: + print "\nInterrupted with CTRL+C" + break clientSocket.close() #Allow the client to give up if no response has been reveived within 1 second. \ No newline at end of file diff --git a/UDP/UDPServer.py b/UDP/UDPServer.py index f28fa98..a036822 100644 --- a/UDP/UDPServer.py +++ b/UDP/UDPServer.py @@ -3,6 +3,8 @@ #UDP (SOCK_DGRAM) is a datagram-based protocol. You send one #datagram and get one reply and then the connection terminates. from socket import socket, SOCK_DGRAM, AF_INET +from random import randint +from time import time #Create a UDP socket #Notice the use of SOCK_DGRAM for UDP packets @@ -14,10 +16,21 @@ while True: # Receive the client packet along with the address it is coming from message, address = serverSocket.recvfrom(2048) + # Get current time, starting immediately after packet is received + startTime = time() + # Randomly drop packets 10% of the time + if randint(0, 9) == 0: + continue # Capitalize the message from the client print message, address message = message.upper() serverSocket.sendto(message, address) + # Get final time + endTime = time() + # Calculate elapsed time in milliseconds and display as RTT + # (the resolution of time() is too large to capture very small times on Windows) + elapsedTime = (endTime - startTime) * 1000 + print "RTT: " + str(elapsedTime) + " ms" serverSocket.close()