UDP Client/Server

Discussion in 'Python' started by Martin Marcher, Jan 22, 2008.

  1. Hello,

    I created a really simple udp server and protocol but I only get every 2nd
    request (and thus answer just every second request).

    Maybe someone could shed some light, I'm lost in the dark(tm), sorry if this
    is a bit oververbose but to me everything that happens here is black magic,
    and I have no clue where the packages go. I can't think of a simpler
    protocol than to just receive a fixed max UDP packet size and answer
    immediately (read an "echo" server).

    thanks
    martin


    ### server
    >>> from socket import *
    >>> import SocketServer
    >>> from SocketServer import BaseRequestHandler, UDPServer
    >>> class FooReceiveServer(SocketServer.UDPServer):

    .... def __init__(self):
    .... SocketServer.UDPServer.__init__(self, ("localhost", 4321),
    FooRequestHandler)
    ....
    >>> class FooRequestHandler(BaseRequestHandler):

    .... def handle(self):
    .... data, addr_info = self.request[1].recvfrom(65534)
    .... print data
    .... print addr_info
    .... self.request[1].sendto("response", addr_info)
    ....
    >>> f = FooReceiveServer()
    >>> f.serve_forever()

    request 0
    ('127.0.0.1', 32884)
    request 1
    ('127.0.0.1', 32884)
    request 2
    ('127.0.0.1', 32884)
    request 2
    ('127.0.0.1', 32884)
    request 2
    ('127.0.0.1', 32884)



    ### client
    >>> target = ('127.0.0.1', 4321)
    >>> from socket import *
    >>> s = socket(AF_INET, SOCK_DGRAM)
    >>> for i in range(10):

    .... s.sendto("request " + str(i), target)
    .... s.recv(65534)
    ....
    9
    Traceback (most recent call last):
    File "<stdin>", line 3, in <module>
    KeyboardInterrupt
    >>> s.sendto("request " + str(i), target)

    9
    >>> str(i)

    '0'
    >>> for i in range(10):

    .... s.sendto("request " + str(i), target)
    .... s.recv(65534)
    ....
    9
    'response'
    9
    'response'
    9
    Traceback (most recent call last):
    File "<stdin>", line 3, in <module>
    KeyboardInterrupt
    >>> #this was hanging, why?

    ....
    >>> s.sendto("request " + str(i), target)

    9
    >>> s.recv(65534)

    'response'
    >>> s.sendto("request " + str(i), target)

    9
    >>> s.recv(65534)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyboardInterrupt
    >>> s.sendto("request " + str(i), target)

    9
    >>> s.sendto("request " + str(i), target)

    9
    >>> s.recv(65534)

    'response'
    >>> s.recv(65534)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyboardInterrupt
    >>> s.sendto("request " + str(i), target)

    9
    >>>


    --
    http://noneisyours.marcher.name
    http://feeds.feedburner.com/NoneIsYours

    You are not free to read this message,
    by doing so, you have violated my licence
    and are required to urinate publicly. Thank you.
     
    Martin Marcher, Jan 22, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sarath
    Replies:
    0
    Views:
    397
    sarath
    Mar 11, 2006
  2. Jack Klein
    Replies:
    0
    Views:
    459
    Jack Klein
    Aug 4, 2003
  3. Adam M.
    Replies:
    1
    Views:
    362
    Monisha Barooah
    Aug 4, 2003
  4. WIWA

    UDP client-server problem

    WIWA, Jul 15, 2003, in forum: Python
    Replies:
    2
    Views:
    695
    Heiko Wundram
    Jul 16, 2003
  5. udp multicast server and client

    , Apr 25, 2008, in forum: C Programming
    Replies:
    3
    Views:
    838
    Sri Harsha Dandibhotla
    Apr 25, 2008
Loading...

Share This Page