Socket Troubles

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

    #1

    Socket Troubles

    I've written a simple class to manage P2P socket connections. However,
    whenever I try to receive data, the socket raises an exception with the
    error message (11, 'Resource temporarily unavailable').

    My code's fairly straight-forward, with much of it right out of the
    Python docs, so I'm not sure what I'm doing wrong. You can see it all at


    Any help is immensely appreciated.

    Thanks,
    Chris
  • Chris Spencer

    #2
    Re: Socket Troubles

    Chris Spencer wrote:[color=blue]
    > I've written a simple class to manage P2P socket connections. However,
    > whenever I try to receive data, the socket raises an exception with the
    > error message (11, 'Resource temporarily unavailable').
    >
    > My code's fairly straight-forward, with much of it right out of the
    > Python docs, so I'm not sure what I'm doing wrong. You can see it all at
    > http://deadbeefbabe.org/paste/1525/0
    >
    > Any help is immensely appreciated.
    >
    > Thanks,
    > Chris[/color]

    One more thing. The code I posted is also a complete demo of my problem.
    Run the script in two different terminals simultaneously, like
    script.py 8000 8001
    and
    script.py 8001 8000

    They should then try talking to each other, reproducing the problem.

    Chris

    Comment

    • Peter Hansen

      #3
      Re: Socket Troubles

      Chris Spencer wrote:[color=blue]
      > I've written a simple class to manage P2P socket connections. However,
      > whenever I try to receive data, the socket raises an exception with the
      > error message (11, 'Resource temporarily unavailable').[/color]

      I would assume (without looking at your code) that this is equivalent to
      the Windows error "WSAEWOULDBLOCK " (10035) for which Microsoft's docs
      say this:

      Resource temporarily unavailable.
      This error is returned from operations on nonblocking sockets that
      cannot be completed immediately, for example recv when no data is queued
      to be read from the socket. It is a nonfatal error, and the operation
      should be retried later. It is normal for WSAEWOULDBLOCK to be reported
      as the result from calling connect on a nonblocking SOCK_STREAM socket,
      since some time must elapse for the connection to be established.


      Does that help? If not, at least provide information about what
      platform you're running on, and preferably post the code, if you can
      reduce it to only a few lines which still reproduces the problem. Many
      people don't like to spend time downloading code from who knows where
      and attempting to run it on their own machine, but could find the
      problem with a quick inspection of your code if posted here (but not if
      it's hundreds of lines!).

      -Peter

      Comment

      • Michael Sparks

        #4
        Re: Socket Troubles

        Chris Spencer wrote:[color=blue]
        > My code's ... at http://deadbeefbabe.org/paste/1525/0[/color]
        ....[color=blue]
        > I've written a simple class to manage P2P socket connections. However,
        > whenever I try to receive data, the socket raises an exception with the
        > error message (11, 'Resource temporarily unavailable').[/color]

        At one point in your code you do this:
        self._socket.se tblocking(0)

        This says "if we can't recieve or send data without blocking, fail rather
        than succeed". One of the failure modes is to return error 11. This is
        infact normally defined as:
        #define EAGAIN 11 /* Try again */

        What this means is "sorry, I couldn't do this without blocking right now,
        try again very shortly!".

        Looking at your code it continually loops (in BaseConnectionH andler_recv)
        receiving data - whether or not there's any data to receive:
        def _recv(self):
        while self.running:
        try:
        data = self._socket.re cv(4096)
        if not len(data):
        time.sleep(0.1)
        continue
        except Exception, e:
        log('Recieve failed for handler',self.a ddress,'because of',e)

        break

        Since you never actually check to see if the socket is ready to give you
        data, and you've set it non-blocking, seeing lots of EAGAIN errors is
        pretty much what you'd expect to see. (Simply sleeping is not sufficient!)

        I suppose the short answer though really is this: you set the socket
        non-blocking, you should therefore expect to see failures telling you
        to try again, and follow their advice! :)

        Regards,


        Michael.

        Comment

        • Dennis Lee Bieber

          #5
          Re: Socket Troubles

          On Sun, 28 Aug 2005 08:12:57 -0400, Peter Hansen <peter@engcorp. com>
          declaimed the following in comp.lang.pytho n:
          [color=blue]
          > Chris Spencer wrote:[color=green]
          > > I've written a simple class to manage P2P socket connections. However,
          > > whenever I try to receive data, the socket raises an exception with the
          > > error message (11, 'Resource temporarily unavailable').[/color]
          >
          > I would assume (without looking at your code) that this is equivalent to
          > the Windows error "WSAEWOULDBLOCK " (10035) for which Microsoft's docs
          > say this:
          >[/color]
          In my experience, winsock error codes map directly to common socket
          error numbers -> 10035 is 35... 11 would then be 10011.

          However, a scan of the relevant .H files shows that (100)11 is NOT
          one of the winsock reported variations.
          --[color=blue]
          > =============== =============== =============== =============== == <
          > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
          > wulfraed@dm.net | Bestiaria Support Staff <
          > =============== =============== =============== =============== == <
          > Home Page: <http://www.dm.net/~wulfraed/> <
          > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

          Comment

          • Chris Spencer

            #6
            Re: Socket Troubles

            Michael Sparks wrote:[color=blue]
            > Chris Spencer wrote:
            >
            > At one point in your code you do this:
            > self._socket.se tblocking(0)
            >
            > This says "if we can't recieve or send data without blocking, fail rather
            > than succeed". One of the failure modes is to return error 11. This is
            > infact normally defined as:
            > #define EAGAIN 11 /* Try again */
            >
            > What this means is "sorry, I couldn't do this without blocking right now,
            > try again very shortly!".
            >
            > Looking at your code it continually loops (in BaseConnectionH andler_recv)
            > receiving data - whether or not there's any data to receive:
            > def _recv(self):
            > while self.running:
            > try:
            > data = self._socket.re cv(4096)
            > if not len(data):
            > time.sleep(0.1)
            > continue
            > except Exception, e:
            > log('Recieve failed for handler',self.a ddress,'because of',e)
            >
            > break
            >
            > Since you never actually check to see if the socket is ready to give you
            > data, and you've set it non-blocking, seeing lots of EAGAIN errors is
            > pretty much what you'd expect to see. (Simply sleeping is not sufficient!)
            >
            > I suppose the short answer though really is this: you set the socket
            > non-blocking, you should therefore expect to see failures telling you
            > to try again, and follow their advice! :)[/color]

            You're quite right. I fixed this by using select(). However, I was still
            having problems with open() blocking the main thread. Then I realized a
            slight problem:
            t = threading.Threa d(target=self._ connection_hand ler(h))

            I changed this to:
            t = threading.Threa d(target=self._ connection_hand ler, args=(h,))
            and now it appears to be working correctly.

            Thanks for your help! I truly appreciate it.

            Chris

            Comment

            Working...