One more socket programming question

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

    One more socket programming question

    I'm now experimenting with the SocketServer class. Originally I
    subclassed the StreamRequestHa ndler to make my own custom handler, but a
    result of this seems to be that the client socket closes after it has
    been used, instead of staying open.

    Just as a test, I decided to use BaseRequestHand ler instead, because I
    know its methods aren't implemented. So this is what I have:

    -----
    import SocketServer

    host = ''
    port = 51234
    address = (host, port)
    buffer_size = 1024

    class MyRequestHandle r(SocketServer. BaseRequestHand ler):
    def handle(self):
    print '...connected from:', self.client_add ress
    data = self.request.re cv(buffer_size)
    self.request.se nd('%s %s' % ('You typed:', data))

    socket_server = SocketServer.TC PServer(address , MyRequestHandle r)
    print 'waiting for connection...'
    socket_server.s erve_forever()
    ------

    ------
    from socket import *

    host = 'localhost'
    port = 51234
    address = (host, port)
    buffer_size = 1024

    client_socket = socket(AF_INET, SOCK_STREAM)
    client_socket.c onnect(address)

    while True:
    data = raw_input('')
    if not data:
    break
    client_socket.s end(data)
    data = client_socket.r ecv(buffer_size )
    print data

    client_socket.c lose()
    ------

    But this only seems to work one time, and then subsequent attempts
    return nothing, and then the client program seems to crash (or just
    close on its own).

    What's happening here?
  • Tim Roberts

    #2
    Re: One more socket programming question

    John Salerno <johnjsal@gmail NOSPAM.comwrote :
    >
    >I'm now experimenting with the SocketServer class. Originally I
    >subclassed the StreamRequestHa ndler to make my own custom handler, but a
    >result of this seems to be that the client socket closes after it has
    >been used, instead of staying open.
    Right. "handle" is not called for one REQUEST at a time, it's called for
    one CONNECTION at a time. If you need a connection to be persistent, then
    your handle() function needs to sit in a loop making recv calls until you
    detect that the conversation is complete.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

    Comment

    • John Salerno

      #3
      Re: One more socket programming question

      "Tim Roberts" <timr@probo.com wrote in message
      news:sjdh541b2u 7m789i8ef6rmple 5smklbt9i@4ax.c om...
      John Salerno <johnjsal@gmail NOSPAM.comwrote :
      >>
      >>I'm now experimenting with the SocketServer class. Originally I
      >>subclassed the StreamRequestHa ndler to make my own custom handler, but a
      >>result of this seems to be that the client socket closes after it has
      >>been used, instead of staying open.
      >
      Right. "handle" is not called for one REQUEST at a time, it's called for
      one CONNECTION at a time. If you need a connection to be persistent, then
      your handle() function needs to sit in a loop making recv calls until you
      detect that the conversation is complete.
      Ah, so I need to rewrite my handle method. I was thinking that the
      specialized setup() and/or finish() calls from StreamRequestHa ndler were the
      reason that the connection was closed after one use (which is why I tried
      BaseRequestHand ler instead).

      Thanks.


      Comment

      Working...