udp, datagram sockets

anethema jefishman at gmail.com
Mon Aug 6 22:14:39 CEST 2007


On Aug 6, 1:27 pm, Carsten Haese <cars... at uniqsys.com> wrote:
> On Mon, 2007-08-06 at 09:03 -0700, 7stud wrote:
> > server:
> > ------------------
> > import socket
> > import sys
>
> > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> > s.bind(("", 7777))
> > [...]
> >         #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)
>
> I don't think that sending the datagram to port 7777 on localhost sends
> the message back to the client. I'm guessing the server is sending the
> message back to itself, which throws it into the infinite feedback loop
> you're experiencing.
>
> HTH,
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

Yes, you want to use the socket.recvfrom() method, store the address,
and send the response to that address.  Your code is indeed repeatedly
sending itself the received message.

You want to change
> >             data  = s.recv(1024)
to
            data, recv_addr = s.recvfrom(1024)

and
> >             size_sent = s.sendto(message[total_sent:], ("localhost",
> > 7777) )
to
            size_sent = s.sendto(message[total_sent:], recv_addr)

However, your client closes immediately after sending all its data, so
it will never receive that message.




More information about the Python-list mailing list