strange socket behaviour

J

Joe Wong

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
 
F

fishboy

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

Chris Reay

fishboy said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top