use select() to listen on both tcp and udp message

12,698
  1. Get rid of the 'while' loops in read_tcp() and read_udp(). The select() loop is the only loop you need: it will call the read_XXX() methods as often as required. The read_XXX() methods should handle exactly one event.

  2. Your read_tcp() method should be split into two parts: one to accept a socket and add it to the selection set, and another to read an accepted socket. Adjust the select loop accordingly.

Share:
12,698
lgwest
Author by

lgwest

Updated on June 04, 2022

Comments

  • lgwest
    lgwest almost 2 years

    I only get the TCP message when I try this code:

    from socket import *
    from select import select
    
    def read_tcp(s):
        while True:
            client,addr = s.accept()
            data = client.recv(8000)
            client.close()
            print "Recv TCP:'%s'" % data
    
    def read_udp(s):
        while True:
            data,addr = s.recvfrom(8000)
            print "Recv UDP:'%s'" % data
    
    def run():
        host = ''
        port = 8888
        size = 8000
        backlog = 5
    
        # create tcp socket
        tcp = socket(AF_INET, SOCK_STREAM)
        tcp.bind(('',port))
        tcp.listen(backlog)
    
        # create udp socket
        udp = socket(AF_INET, SOCK_DGRAM)
        udp.bind(('',port))
    
        input = [tcp,udp]
    
        while True:
            inputready,outputready,exceptready = select(input,[],[])
    
            for s in inputready:
                if s == tcp:
                    read_tcp(s)
                elif s == udp:
                    read_udp(s)
                else:
                    print "unknown socket:", s
    
    if __name__ == '__main__':
        run()
    

    And the client is like this:

    from socket import *
    
    def send_tcp():
        s = socket(AF_INET,SOCK_STREAM)
        s.connect(('localhost',8888))
        data="TCP "*4
        s.send(data)
        s.close()
    
    def send_udp():
        s = socket(AF_INET,SOCK_DGRAM)
        data="UDP "*4
        s.sendto(data, ('localhost',8888))
        s.close()
    
    if __name__ == '__main__':
        send_tcp()
        send_udp()