socket.sendto / UDP problem

MRAB python at mrabarnett.plus.com
Wed Oct 20 19:07:58 EDT 2010


On 20/10/2010 21:20, Todd Walter wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello,
>
> When transmitting via UDP to a PLC, I run into a strange problem
> where socket.sendto returns double the number of characters sent in the
> datagram.  I thought this was an error and used Wireshark to sniff the
> connection and discovered that it did, in fact, include two copies of
> the string I am transmitting in the same packet.  The only
> thing differentiating this string from prior commands is its length.
> The functional ones are between 5&  7 bytes (with 16 byte responses
> received successfully) but transmitting a 66-byte message actually
> results in 132 bytes being sent!
>
> I am running 2.6.6 on Windows XP, which I understand has a default
> minimum buffersize of 576 bytes which I would think is sufficient.
>
> Apologies if this is somewhat incoherent, I'm cross-eyed from staring
> at this!
>
[snip]

The docs for 'sendto' say:

     """The socket should not be connected to a remote socket, since the
     destination socket is specified by address."""

Could your problem be caused by you binding the socket to a source
port, so it's going out both to the bound port _and_ the one given the
binding?

Have you tried using two sockets, one outgoing and the other incoming?

BTW, your code for handling the response doesn't cope with it coming in
a bit at a time. It loops discard any previous data from the previous
iteration.

Also, it's more Pythonic to say:

     while '\r' not in response:
         ...



More information about the Python-list mailing list