strange socket behaviour

fishboy fishboy at spamspamspam.com
Wed Jun 2 09:23:30 EDT 2004


On Wed, 2 Jun 2004 10:37:21 +0800, "Joe Wong" <joewong at mango.cc>
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.

><{{{*>




More information about the Python-list mailing list