I have to write a client and a server program with python were the user gets ask if he would like to send via TCP or UDP. The Server simply sends back the data because i want to find out how long it takes (RTT).
How can the server detect either the client sends TCP or UPD Data?
This is just for an educational purpose, not meant to be used in production.
I have the following code so far:
client.py
import socket
numerofpackets = int(raw_input("How many packets should be sent?\n> "))
connectiontype = raw_input("TCP or UDP?\n> ")
hostname = 'localhost'
i = 0
if connectiontype != "TCP" and connectiontype != "UDP":
print "Input not valid. Either type TCP or UDP"
else:
if connectiontype == "TCP":
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((hostname, 50000))
while i < numerofpackets:
start_time = time.time()
s.send('tcp')
response = s.recv(1024)
print time.time() - start_time
i = i + 1
s.close()
elif connectiontype == "UDP":
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while i < numerofpackets:
start_time = time.time()
s.sendto('udp', (hostname, 50000))
response, serverAddress = s.recvfrom(1024)
print time.time() - start_time
i = i + 1
s.close()
and server:
#for tcp
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 50000))
s.listen(1)
while True:
komm, addr = s.accept()
data = komm.recv(1024)
komm.send(data)
s.close()
#for udp
sudp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sudp.bind(("", 50000))
while True:
daten, addr = sudp.recvfrom(1024)
s.sendto(daten, addr)
s.close()