Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.linting.enabled": false
}
3 changes: 3 additions & 0 deletions TCP/runServer.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
python TCPServer.py
pause
21 changes: 16 additions & 5 deletions UDP/UDPClient.py
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions UDP/UDPServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()


Expand Down
3 changes: 3 additions & 0 deletions UDP/runServer.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
python UDPServer.py
pause