2

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

1 Answer 1

1

The server will have to listen on both UDP & TCP. You can use two server processes, threads or block your server with select.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. I separeted the TCP and UDP code into 2 files and now run both servers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.