socket.sendto / UDP problem

Todd Walter twalter at rogers.com
Fri Oct 22 15:27:51 CEST 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, 22 Oct 2010 00:00:03 +0100
MRAB <python at mrabarnett.plus.com> wrote:

> On 21/10/2010 21:05, Todd Walter wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > On Thu, 21 Oct 2010 17:03:58 +0100
> > MRAB<python at mrabarnett.plus.com>  wrote:
> >
> >> On 21/10/2010 15:57, Todd Walter wrote:
> >>> -----BEGIN PGP SIGNED MESSAGE-----
> >>> Hash: SHA1
> >>>
> >>> On Thu, 21 Oct 2010 00:07:58 +0100
> >>> MRAB<python at mrabarnett.plus.com>   wrote:
> >>>
> >>>>>
> >>>> [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 respo
> >>>>            ...
> >>> I haven't bound the socket to a remote port, as I read it; it'sp
> >>> bound to a source port (192.168.10.2:2260, the local machine) and
> >>> just transmits to an address with a port glommed onu sn
> >>> (192.168.10.1:2002, the PLC).
> >> [snip]
> >> What I meant was that you're using 'pcSocket' for both directions
> >> and using .bind on it.
> >>
> >> Try creating two sockets, 'pcInSocket' and 'pcOutSocket', and bind
> >> only pcOutSocket.

As it turns out, I must use only one socket.  I have to use a fixed
source port as the PLC will respond to that port.  If I transmit
using .sendto with an unbound socket the source port is picked at
random making listening for the response impossible. It was my
understanding that there could be a many-to-one relationship between
sockets and ports but an attempt to recv without a bind throws an error
and an attempt to bind to an already bound port throws an error.  I
thought the socket operations worked on the buffer so why does
multiplexing fail?  It shouldn't care how many listeners there are, it
should just read the information off the wire and throw it somewhere
everyone can read it.  Is there a way to specify the source port for a
transmission without first binding to it?

- - Todd
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)

iEYEARECAAYFAkzBkV0ACgkQwnknPuQqPIM/4ACeKDGYAUJPdBjyGV2Iu6l/5bA1
X/MAoIWDOvnMhdA0NHXLo2Mv1Nm8kkZZ
=4t/0
-----END PGP SIGNATURE-----


More information about the Python-list mailing list