Hi,

I'm writing a simple echo/chat GUI server using wxPython. I made a separate thread for the server. The only problem I have is that when I try to close the server it hangs in socket.recv() thus hanging the whole thread. How can stop the socket from receiving from the GUI thread?

Thanks

PS: here is the socket thread:

class echoThread(threading.Thread):
    #---------socket variables |
    #------------------------- |
    backlog = 5 
    size = 1024 
    
    def run(self):
       # while running==1:
        global client
        global server
        server.bind ( ( '', 5000 ) )
        server.listen ( 5 )
        client, address = server.accept()
                                    
        while  client: 
            data = client.recv(1024) 
            if data: 
                if mainFrame.setecho.IsChecked():
                        client.send(data)
                        mainFrame.console.AppendText(data)

The usage depends on what interface you're using to make your client/server (I've only ever used paramiko); however you could probably make use of a global timeout setting. This way after a set amount of time the recv() operation will throw a timeout exception, which you can catch and handle however you would like.

got a short example?

c = t.open_session()
if timeOut != None:
    c.settimeout(timeOut)

This is an excerpt from a function that sends a command to the Server. Keep in mind that I'm using paramiko here, so it probably won't work for you unless you too are using paramiko.

I have absolutely no idea what is paramiko, but i'll google it. :)
Anyway that's all I needed to give me an idea on what to google for :D
... i'll post my solution ...sometime soon :)
Thanks

ha, i've done it! :cool: :D

it's probably not the most elegant solution but for me it's a masterpiece!:))

class echoThread(threading.Thread):
    
    def run(self):
        server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM)
        global client
        server.bind ( ( '', 5000 ) )
        server.listen ( 5 )
        server.settimeout(1)

        address='reason to loop'

        while address=='reason to loop':
            try:
                client, address = server.accept()
                print client
                print address
            except socket.timeout:
                if running==0:
                    break

        client.settimeout(1)

        while  running==1:
            try:
                data = client.recv(1024)
                if data:
                    if mainFrame.setecho.IsChecked():
                        client.send(data)
                        mainFrame.console.AppendText(data)
            except socket.timeout:
                if running==0:
                    break

Thanks jlm699

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.