strange socket behaviour

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

    strange socket behaviour

    Hello,

    I have a short program that the client make a connection to server, a thread is then created to poll any data sent from the server. The main thread will close the socket after 5 seconds. Here are the code:

    from socket import *
    import select
    import threading
    import time

    def poll(c):
    i, o, e = select.select([c], [], [])
    if not i:
    print "time out"
    return
    print i
    data = i[0].recv(1024)
    print "data: ", data

    if __name__=="__ma in__":
    c = socket(AF_INET, SOCK_STREAM)
    c.connect(('192 .168.100.74', 8888))
    th=threading.Th read(None, poll, "", (c, ))
    th.setDaemon(1)
    th.start()

    time.sleep(5)
    c.shutdown(2)
    c.close()
    th.join()
    print "completed"

    On Windows, as soon as client socket 'c' is closed, the select() call returns. However, on Linux, the program seems blocking forever ( may be I am not patient to wait ). Is there anything wrong with my code?

    Regards,

    -- Wong

  • fishboy

    #2
    Re: strange socket behaviour

    On Wed, 2 Jun 2004 10:37:21 +0800, "Joe Wong" <joewong@mango. cc>
    wrote:
    [color=blue]
    >Hello,
    >
    > I have a short program that the client make a connection to server, a thread is then created to poll any data sent from the server. The main thread will close the socket after 5 seconds. Here are the code:
    >
    >from socket import *
    >import select
    >import threading
    >import time
    >
    >def poll(c):
    > i, o, e = select.select([c], [], [])
    > if not i:
    > print "time out"
    > return
    > print i
    > data = i[0].recv(1024)
    > print "data: ", data
    >
    >if __name__=="__ma in__":
    > c = socket(AF_INET, SOCK_STREAM)
    > c.connect(('192 .168.100.74', 8888))
    > th=threading.Th read(None, poll, "", (c, ))
    > th.setDaemon(1)
    > th.start()
    >
    > time.sleep(5)
    > c.shutdown(2)
    > c.close()
    > th.join()
    > print "completed"
    >
    >On Windows, as soon as client socket 'c' is closed, the select() call returns. However, on Linux, the program seems blocking forever ( may be I am not patient to wait ). Is there anything wrong with my code?
    >
    >Regards,
    >
    >-- Wong[/color]

    No, your code is ok. It's just that closing the local end of the
    socket is undefined in select(). Which is why Windows does one thing
    and Linux the other.

    Closing the remote end causes the socket to show up as readable with
    zero data.
    [color=blue]
    ><{{{*>[/color]

    Comment

    • Chris Reay

      #3
      Re: strange socket behaviour

      fishboy <fishboy@spamsp amspam.com> wrote in message news:<82jrb0t5s s1dtf9qki44qphd 13t0vpnl02@4ax. com>...[color=blue]
      > On Wed, 2 Jun 2004 10:37:21 +0800, "Joe Wong" <joewong@mango. cc>
      > wrote:
      >[/color]
      snip[color=blue]
      >
      > No, your code is ok. It's just that closing the local end of the
      > socket is undefined in select(). Which is why Windows does one thing
      > and Linux the other.
      >
      > Closing the remote end causes the socket to show up as readable with
      > zero data.
      >[/color]

      Precisely. I've been through this myself. My solution on the FreeBSD
      server side is (pardon the bad pseudocode):

      rd, wr, err = select(myClient SocksList, [], myClientSocksLi st,
      myTimeOut)
      for eachSock in rd:
      try:
      dataIn = eachSock.recv(M axBufSz)
      except socket.error:
      # Server closes session.
      self.closeSessi on(eachSock, "Socket error - recv")
      else:
      if len(dataIn) > 0:
      # Process, process.
      else:
      # Server closes session.
      self.closeSessi on(eachSock, "Recvd 0 bytes")
      # Check the err list etc, etc.

      Comment

      Working...