client derived from async_chat

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

    client derived from async_chat

    Has anybody got such an example? My server works fine, but my client
    does not :-(

    --
    Real e-mail address is 'cHVAdm8ubHU=\n '.decode('base6 4')
    Visit my Homepage at http://www.homepages.lu/pu/

  • Patrick Useldinger

    #2
    Re: client derived from async_chat

    Erik Max Francis wrote:
    [color=blue]
    > I tried to run your sample but you didn't include enough material for me
    > to run it as a standalone application. When I tried to stub out the
    > additional material you didn't include, I got connection refused errors,
    > presumably because the server wasn't running at the point your clients
    > tried to connect.[/color]

    Look at the following code:

    sock=socket.soc ket(socket.AF_I NET,socket.SOCK _STREAM)
    #sock.setblocki ng(0)
    print 'connect rc=',sock.conne ct_ex((EBHost,E BPort))
    try:
    sock.send(messa ge+BLOCKEND)
    response=sock.r ecv(BUFFSIZE)
    finally:
    sock.close()

    If I run it without the 'sock.setblocki ng(0)', it works fine. If
    uncomment that line, I receive the following error:

    File "I:\My Programs\sas\sa sLA0.py", line 18, in ?
    response=sock.r ecv(BUFFSIZE)
    socket.error: (10035, 'The socket operation could not complete without
    blocking')

    This is exactly what seems to happen with the async_chat class, as in
    dispatcher.crea te_socket the same setblocking(0) is done.

    If you just could shed some light on what this error means, I haven't
    found any explanation up to now.

    Thanks.

    --
    Real e-mail address is 'cHVAdm8ubHU=\n '.decode('base6 4')
    Visit my Homepage at http://www.homepages.lu/pu/

    Comment

    • Erik Max Francis

      #3
      Re: client derived from async_chat

      Patrick Useldinger wrote:
      [color=blue]
      > This is exactly what seems to happen with the async_chat class, as in
      > dispatcher.crea te_socket the same setblocking(0) is done.
      >
      > If you just could shed some light on what this error means, I haven't
      > found any explanation up to now.[/color]

      It'd doing what you asked it to do. Functions that would block return
      an error of EWOULDBLOCK (which is translated into a socket.error
      exception in Python) if they would have blocked but you have set the
      socket not to block. The error is not an error, it's just the system
      telling you, "I would have blocked here, but you told me not to, so I'm
      telling you that."

      The bigger question is why you're asking the socket to not block when it
      appears that you really want it to (since your logic isn't handling the
      case when it doesn't).

      --
      Erik Max Francis && [email protected] && http://www.alcyone.com/max/
      __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
      / \ Defeat is a school in which truth always grows strong.
      \__/ Henry Ward Beecher

      Comment

      • Patrick Useldinger

        #4
        Re: client derived from async_chat

        Heiko Wundram wrote:
        [color=blue]
        > Remember: connecting is also a non-blocking operation, if the socket is
        > set to non-blocking mode.[/color]

        I should have read some more about sockets before trying to use
        async_chat; I thought I would get by 'just adding a few methods'. Shame
        on me ;-)

        Anyway, what happened is that 'create_socket( )' and 'connect()' were
        called to quickly one after the other; as async_chat (asnycore,
        actually) sets the socket on creation to non-blocking, my connect failed.

        I solved the problem by adding a method 'connect()' in my derived class
        which looks like this:

        def connect(self,*a rgs):
        timeOut=self.ge ttimeout()
        self.settimeout (None)
        SingleServer.co nnect(self,*arg s)
        self.settimeout (timeOut)

        This did the trick for my chat client.
        [color=blue]
        > I dunno why async_chat doesn't work with this on Windows (on *nix, this
        > is exactly what it does), but I guess it has something to do with
        > decoding the error codes, as a *nix-socket would return EWOULDBLOCK in
        > this case, for which it checks, I am certain of that.[/color]

        I think it works as designed, but it was not designed to open sockets
        for clients, only for servers. I could have opened the socket outside
        the method, probably, but I really wanted the class to handle all of the
        network stuff.

        Anyway, it works fine now.

        Thanks for your explanation.

        -Patrick

        --
        Real e-mail address is 'cHVAdm8ubHU=\n '.decode('base6 4')
        Visit my Homepage at http://www.homepages.lu/pu/

        Comment

        • Patrick Useldinger

          #5
          Re: client derived from async_chat

          Erik Max Francis wrote:
          [color=blue]
          > The bigger question is why you're asking the socket to not block when it
          > appears that you really want it to (since your logic isn't handling the
          > case when it doesn't).[/color]

          See my answer to Heiko; it's the class asyncore which handles the
          (non-)blocking staff, and I relied on that. Now that I've overridden the
          connect() method for my client, it works fine.

          Thanks for your help.

          -Patrick

          --
          Real e-mail address is 'cHVAdm8ubHU=\n '.decode('base6 4')
          Visit my Homepage at http://www.homepages.lu/pu/

          Comment

          Working...