F
flupke
Hi,
I have a gui (made in wxPython) that enables a user to connect to a server
and issue some commands. The problem occurs when i try to disconnect the
client. It exits but it doesn't return to the prompt. I have to push
Ctrl-C in order to have it exit completely. The GUI is closed though.
This is a piece of code from the main class that connects to the server
and starts a thread that handles the connection:
======================= snippet =======================
Create a socket and start a thread to handle the connection:
#create an INET, STREAMing socket
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#now connect to the CheckServer
try:
self.s.connect((host, int(data)))
except socket.error, msg:
sys.stderr.write('connect failed: ' + repr(msg) + '\n')
sys.exit(1)
self.connection = client_connection(self.s, self)
self.connection.start()
======================= snippet =======================
client_connection is the thread that handles the connection.
Next, the client_connection class
======================= snippet =======================
BUF_SIZE = 1024
class client_connection(threading.Thread):
def __init__(self,client_socket,parent):
self.client_socket = client_socket
self.parent = parent
threading.Thread.__init__(self)
def run(self):
done = 0
try:
while not done:
print "ready to receive data"
try:
data = self.client_socket.recv(BUF_SIZE)
except socket.error, msg:
print "Error receiving data"
break
if not data:
print "all data received"
break
print "receiving data"
sys.stdout.write(data)
sys.stdout.flush()
self.parent.handleCommand(data)
finally:
self.client_socket.close()
print "Closing client socket"
def message(self,msg):
print "from client_connection", msg
self.client_socket.sendall(msg+"\n")
def close(self):
#self.message("bye")
done = 1
self.client_socket.settimeout(0)
self.client_socket.close()
======================= snippet =======================
So when the user pushes the exit button, i try to call the close
function of the client_connection class:
======================= snippet =======================
def OnFileExit(self,e):
self.connection.close()
time.sleep(5)
self.Close(true) # Close the frame.
======================= snippet =======================
I thought that this would trigger the
"data=self.client_socket.recv(BUF_SIZE)"
from the client_connection class in such a way that it would produce an
exception or return empty data so i could close the thread gracefuly.
Apparently this doesn't happen.
When the server initiates the closing of the socket, then all happens fine.
How can i solve this & what am i doing wrong?
Thanks,
Benedict
I have a gui (made in wxPython) that enables a user to connect to a server
and issue some commands. The problem occurs when i try to disconnect the
client. It exits but it doesn't return to the prompt. I have to push
Ctrl-C in order to have it exit completely. The GUI is closed though.
This is a piece of code from the main class that connects to the server
and starts a thread that handles the connection:
======================= snippet =======================
Create a socket and start a thread to handle the connection:
#create an INET, STREAMing socket
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#now connect to the CheckServer
try:
self.s.connect((host, int(data)))
except socket.error, msg:
sys.stderr.write('connect failed: ' + repr(msg) + '\n')
sys.exit(1)
self.connection = client_connection(self.s, self)
self.connection.start()
======================= snippet =======================
client_connection is the thread that handles the connection.
Next, the client_connection class
======================= snippet =======================
BUF_SIZE = 1024
class client_connection(threading.Thread):
def __init__(self,client_socket,parent):
self.client_socket = client_socket
self.parent = parent
threading.Thread.__init__(self)
def run(self):
done = 0
try:
while not done:
print "ready to receive data"
try:
data = self.client_socket.recv(BUF_SIZE)
except socket.error, msg:
print "Error receiving data"
break
if not data:
print "all data received"
break
print "receiving data"
sys.stdout.write(data)
sys.stdout.flush()
self.parent.handleCommand(data)
finally:
self.client_socket.close()
print "Closing client socket"
def message(self,msg):
print "from client_connection", msg
self.client_socket.sendall(msg+"\n")
def close(self):
#self.message("bye")
done = 1
self.client_socket.settimeout(0)
self.client_socket.close()
======================= snippet =======================
So when the user pushes the exit button, i try to call the close
function of the client_connection class:
======================= snippet =======================
def OnFileExit(self,e):
self.connection.close()
time.sleep(5)
self.Close(true) # Close the frame.
======================= snippet =======================
I thought that this would trigger the
"data=self.client_socket.recv(BUF_SIZE)"
from the client_connection class in such a way that it would produce an
exception or return empty data so i could close the thread gracefuly.
Apparently this doesn't happen.
When the server initiates the closing of the socket, then all happens fine.
How can i solve this & what am i doing wrong?
Thanks,
Benedict