strange socket behaviour

Discussion in 'Python' started by Joe Wong, Jun 2, 2004.

  1. Joe Wong

    Joe Wong Guest

    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__=="__main__":
    c = socket(AF_INET, SOCK_STREAM)
    c.connect(('192.168.100.74', 8888))
    th=threading.Thread(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
     
    Joe Wong, Jun 2, 2004
    #1
    1. Advertising

  2. Joe Wong

    fishboy Guest

    On Wed, 2 Jun 2004 10:37:21 +0800, "Joe Wong" <>
    wrote:

    >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__=="__main__":
    > c = socket(AF_INET, SOCK_STREAM)
    > c.connect(('192.168.100.74', 8888))
    > th=threading.Thread(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


    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.

    ><{{{*>
     
    fishboy, Jun 2, 2004
    #2
    1. Advertising

  3. Joe Wong

    Chris Reay Guest

    fishboy <> wrote in message news:<>...
    > On Wed, 2 Jun 2004 10:37:21 +0800, "Joe Wong" <>
    > wrote:
    >

    snip
    >
    > 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.
    >


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

    rd, wr, err = select(myClientSocksList, [], myClientSocksList,
    myTimeOut)
    for eachSock in rd:
    try:
    dataIn = eachSock.recv(MaxBufSz)
    except socket.error:
    # Server closes session.
    self.closeSession(eachSock, "Socket error - recv")
    else:
    if len(dataIn) > 0:
    # Process, process.
    else:
    # Server closes session.
    self.closeSession(eachSock, "Recvd 0 bytes")
    # Check the err list etc, etc.
     
    Chris Reay, Jun 2, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,861
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    974
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    554
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    670
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    725
Loading...

Share This Page