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