UDP client-server problem

Discussion in 'Python' started by WIWA, Jul 15, 2003.

  1. WIWA

    WIWA Guest

    Hi,

    I want to make a UDP client server application that is conform to
    RFC868 (Time protocol). Both the UDP client and UDP server are in a
    test phase.

    The question is: when I add "svrsocket.sendto(resultaat, (ip, port))"
    in the UDP server, my application closes while running. When I leave
    it away, it works fine. I really need this statement as the purpose is
    that a client sends sth to the server and the server sends back the
    time ticks.

    Anybody an idea how I can modifu my client/server so that it works?

    Regards,

    Wim


    Here is the server:

    # UDP server example
    import time
    import socket
    import string
    import string

    class Tijd:
    def __init__(self, hours=0,minutes=0,seconds=0):
    self.hours=hours
    self.minutes=minutes
    self.seconds=seconds

    def aantal_seconden(self):
    x = time.time()
    y=(1970, 1, 1, 1, 0, 0, 0, 0, 0)
    y=time.mktime(y)
    resultaat=x-y
    return resultaat

    if __name__=="__main__":
    tijd=Tijd()
    resultaat=tijd.aantal_seconden()
    print 'resultaat',resultaat

    port=37

    svrsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    svrsocket.bind(('', port))

    hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname)
    print 'TOD server is at IP adress: ', ip

    tijd=time.ctime()
    tijd=string.split(time.ctime())
    print 'The current time is', tijd[3]
    print 'Listening for TOD-requests on port %s ...' %port

    while 1:
    data, address = svrsocket.recvfrom(256)
    print 'Received a TOD-request from modem with IP-address %s'
    %address[0]
    print 'Sending back the time to modem with
    IP-address',address[0]
    print "time", resultaat
    svrsocket.sendto(resultaat, (ip, port))


    And here is the client:

    # UDP client example
    import socket
    port=37
    clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    while 1:
    data = raw_input("Type something: ")
    if data:
    clisocket.sendto(data, ("127.0.0.1", port))
    else:

    break
    s.close()
     
    WIWA, Jul 15, 2003
    #1
    1. Advertising

  2. WIWA wrote:

    > The question is: when I add "svrsocket.sendto(resultaat, (ip, port))"
    > in the UDP server, my application closes while running. When I leave
    > it away, it works fine. I really need this statement as the purpose is
    > that a client sends sth to the server and the server sends back the
    > time ticks.


    It would have helped if you were more specific about: "closes while running"?
    In my case (running your code) I get an exception in the server:


    Traceback (most recent call last):
    File "server.py", line 44, in ?
    svrsocket.sendto(resultaat, (ip, port))
    File "<string>", line 1, in sendto
    TypeError: sendto() takes exactly 3 arguments (2 given)


    this pretty much tells you what's wrong. Your sendto() call is faulty.

    The first argument in your case is of type <float> while it should be
    a <string>. So try

    svrsocket.sendto(str(resultaat), (ip, port))

    however, this triggers a loop in your code if your client is on the
    same machine as the server (the server sends out a UDP packet to the
    same port as it itself is listening on, on my machine, it gets back
    its own UDP packet....)

    HTH,
    --Irmen de Jong
     
    Irmen de Jong, Jul 15, 2003
    #2
    1. Advertising

  3. This should work:

    Server
    ======

    import socket
    import time

    # Constants.
    PORT = 37

    # Create and bind the socket which listens for packets.
    srvsocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    svrsocket.bind(('',PORT))

    while 1:
    # Block waiting for packet.
    data, address = svrsocket.recvfrom(256)
    print "Client sent:", data
    print "Client at:", address
    # Got a packet, reply to address packet came from.
    srvsocket.sendto(str(time.time()),address)

    Client
    ======

    import socket

    # Constants.
    PORT = 37

    # Create new socket, let it bind to an OS-chosen port.
    clisocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

    while 1:
    data = raw_input("Enter what the server receives.")
    if data:
    # Send data to server, irrelevant what.
    clisocket.sendto(data,("localhost",PORT))
    # Block waiting for reply.
    data, address = clisocket.recvfrom(256)
    print "Server sent time:", data
    else:
    break

    Look at the above code. There were several errors in your original
    implementation, e.g. that time never got updated while the server ran,
    and several other things. I also don't see the need for a Tijd class.

    HTH!

    Heiko.

    PS: The above code is untested, if there's something wrong, my bad... :)
    PPS: Read the UNIX TCP/IP Network Programming FAQ if you're into socket
    programming! Google for it, I currently don't know the URL...
     
    Heiko Wundram, Jul 16, 2003
    #3
    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:
    403
    sarath
    Mar 11, 2006
  2. Jack Klein
    Replies:
    0
    Views:
    467
    Jack Klein
    Aug 4, 2003
  3. Adam M.
    Replies:
    1
    Views:
    381
    Monisha Barooah
    Aug 4, 2003
  4. Martin Marcher

    UDP Client/Server

    Martin Marcher, Jan 22, 2008, in forum: Python
    Replies:
    0
    Views:
    264
    Martin Marcher
    Jan 22, 2008
  5. udp multicast server and client

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

Share This Page