Re: udp, datagram sockets



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



Relevant Pages

  • udp, datagram sockets
    ... to the server, and then the server infinitely loops over the recvto ... infinite while loop used to check the recv. ... repeatedly outputs the client message over and over again. ... I get the following debug output as ...
    (comp.lang.python)
  • Re: Read from TCP Stream greater then buffer size
    ... If I run it with breakspoint in debug and step thru the loop iterations I ... > interact with a NNTP server to retrieve a list of news groups. ... > blocking on the stream read, the question is how to I control receiving a ...
    (microsoft.public.dotnet.languages.vb)
  • interesting error
    ... the c++ dll that contains the meat of the main loop. ... So I have a timer set ... Debug Error! ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Frame-based exception handling problem on Server 2008
    ... To debug debug the issue, ... The endless loop occurs as soon as the PC is at ... typedef struct _exception_list ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Bug with Chart.Name?
    ... .Chartobjects.Count loop ... Dim shp As Shape, chtObj As ChartObject, cht As Chart ... processed as you can see in the debug output, ...
    (microsoft.public.excel.charting)