udp, datagram sockets

Thomas Jollans thomas at jollans.com
Mon Aug 6 18:59:44 CEST 2007


On Monday 06 August 2007, 7stud wrote:
> I'm trying to understand datagrams.  My client program sends a message
> to the server, and then the server infinitely loops over the recv() to
> make sure all the data was received.  I'm trying to use an * to signal
> the end of the message, so that the server can break out of the
> infinite while loop used to check the recv().  However, my server
> repeatedly outputs the client message over and over again.  Can anyone
> tell me why?
>
> When I start the server, I get the following debug output as
> expected:
>
> ---------
> debug: start outer while loop
> #this infinite while loop endlessly listens for messages from the
> client
>
> debug: recv while loop
> #this infinite while loop checks to make sure the whole message was
> received
> -----------
>
> and then the recv() blocks and waits for data to be sent by the
> client. But when I start the client, the server repeatedly outputs the
> client message over and over again.   My expectation was that after
> processing the message from the client, the server would block the
> next time it executed the recv() statement and wait for more data from
> the client.
>
> client:
> ----------
> import socket
>
> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>
> message = "hello*" #asterisk to delimit end of message
> msg_size = len(message)
> total_sent = 0
>
> print "debug:", total_sent, msg_size
>
> while total_sent < msg_size:
>     size_sent = s.sendto(message[total_sent:], ("localhost", 7777) )
>     total_sent += size_sent
>
>     print "debug:", total_sent, msg_size
>
> print 'debug: while loop ended'
> s.close()
> -----------
>
> Here's the client output:
>
> -------------
> debug: 0 6
> debug: 6 6
> debug: while loop ended
> ---------------
>
>
> server:
> ------------------
> import socket
> import sys
>
> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> s.bind(("", 7777))
>
> try:
>     while True:  #This loop listens endlessly for messages.
>         print "debug: start outer while loop"
>
>         #Receive messages from client:
>         my_list = []
>         while True:  #This loop checks to see if the whole message was
> received.
>             print "debug: recv while loop"
>
>             data  = s.recv(1024)
>             my_list.append(data)
>
>             if data.rfind("*"):
>                 message = "".join(my_list)
>                 print "Received message:\n%s" % message[:-1]
>                 break
>
>
>         #Send messages back to client:
>         total_sent = 0
>         while total_sent < len(message):
>             print "debug: send while loop"
>
>             size_sent = s.sendto(message[total_sent:], ("localhost",
> 7777) )
>             total_sent += size_sent
>
>             print "debug:", total_sent, len(message)
>
> finally:
>     s.close()
> --------------------------
>
> Here's the server output:
>
> ---------------
> debug: start outer while loop
> debug: recv while loop
> Received message:
> hello
> debug: send while loop
> debug: 6 6
> debug: start outer while loop
> debug: recv while loop
> Received message:
> hello
> debug: send while loop
> debug: 6 6
> ....
> ....
> ....
> ------------------

You don't make any attempt to break out of the outer loop. (break breaks the 
innermost loop)



More information about the Python-list mailing list