Socket programming design problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    #1

    Socket programming design problem

    After programming with Python for a few hours, I've come up with some code:
    http://p.shurl.net/3n. However, now I've realised there's a bit of a
    problem with the original design.

    I have a number of questions, if anybody could answer I'd be grateful.

    a) Big problem, I can't see how to receive from more than one socket at
    once. I need to do this so that data from the TCP connection can be sent
    out on the UDP one and vice versa. Do I need a thread for each, or is
    there some other way I can listen on two sockets at once (maybe
    non-blocking ones?)

    b) If the script dies prematurely, e.g. I mistyped something and Python
    throws an exception, it is caught by the generic handler at the bottom.
    However, the socket does not seem to be cleaned up properly, as if I try to
    run it again immediately I get an error that it is still in use. lsof
    doesn't show it in use though, which seems weird. Is there any particular
    reason for this?

    Thanks,

    David
  • John J. Lee

    #2
    Re: Socket programming design problem

    David <null@example.n et> writes:
    [...][color=blue]
    > a) Big problem, I can't see how to receive from more than one socket at
    > once. I need to do this so that data from the TCP connection can be sent
    > out on the UDP one and vice versa. Do I need a thread for each, or is
    > there some other way I can listen on two sockets at once (maybe
    > non-blocking ones?)[/color]

    1. threads, yes
    2. http://docs.python.org/lib/module-select.html (if on non-Windows system)
    3. http://docs.python.org/lib/module-asyncore.html (ditto)
    4. http://twistedmatrix.com/


    Interesting but maybe not production-ready code:

    1. http://kamaelia.sourceforge.net/Home
    2. http://poshmodule.sourceforge.net/

    [color=blue]
    > b) If the script dies prematurely, e.g. I mistyped something and Python
    > throws an exception, it is caught by the generic handler at the bottom.
    > However, the socket does not seem to be cleaned up properly, as if I try to
    > run it again immediately I get an error that it is still in use. lsof
    > doesn't show it in use though, which seems weird. Is there any particular
    > reason for this?[/color]

    Google for SO_REUSEADDR



    John

    Comment

    • Dan Sommers

      #3
      Re: Socket programming design problem

      On Thu, 22 Dec 2005 22:12:09 +0000,
      David <null@example.n et> wrote:
      [color=blue]
      > a) Big problem, I can't see how to receive from more than one socket
      > at once. I need to do this so that data from the TCP connection can
      > be sent out on the UDP one and vice versa. Do I need a thread for
      > each, or is there some other way I can listen on two sockets at once
      > (maybe non-blocking ones?)[/color]

      Try the select module:



      Regards,
      Dan

      --
      Dan Sommers
      <http://www.tombstoneze ro.net/dan/>

      Comment

      Working...