Re: [Tutor] seeking help to a problem w/ sockets
It sounds like a process is still listening on the port. If you're on a Unix system then you can use lsof (aka list open files) to locate the process holding on the socket. Killing the process should free the socket. Also, you don't have to close the socket after every connection completes. Try looking at the SocketServer module. It takes care of a lot of the details for you. - Jeff Younker - [EMAIL PROTECTED] - On Apr 26, 2008, at 5:56 PM, James Duffy wrote: I have a problem w/ a file transfer receiver. They way it works is it binds a port for incoming transfer , when the file transfer is complete. It closes the connection and the socket, then loops back and restarts the bind and listen. I have it set so that the socket is reuseable, which is why this works. However, if the program that is using this function is closed while listening, it appears that it does not ”un-bind” because when the program is reopened and a listen attepted to start I get a “port already in use” error. Only a reboot fixes this issue. This code is imported into a main GUI script. We have it set to execute some cleanup functions on exit, I need a function that can dig down to the thread the listener is running in, stop the listen and close the connection and socket. I basically need to get to the close function and then stop the while loop. Thanks in advance for any help anyone can give. My code for the listener class follows: class Reciever ( Thread ): # the reciever class for the test tool, runs as a separate thread from the main program def __init__( this ): Thread.__init__( this ) def run(this): this.process() def bindsock( this ): # create a new socket, bid the socket to the port, listen until connection recieved this.Lport=listen this.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) this.sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) this.sock.bind(('',this.Lport)) this.sock.listen(1) print "Listening on port " +str(this.Lport) def acceptsock( this ): #when connection is incoming, accept the connection this.conn, this.addr = this.sock.accept() print 'Got connection from', this.addr def transfer( this ): #when connection is full established, begin data download global filenumber print 'Starting media transfer ' openfile="XMLrecieved"+str(filenumber)+".xml" f = open(openfile,"wb") #create and open a new file for writing incoming data to while 1: data = this.conn.recv(1024) #check for incoming data if not data: break #if not present, break the loop f.write(data) #if data is present, write it to file and loop back f.close() #when loop is broken, close the file print "Got XML file:" + openfile print 'Closing media transfer' filenumber = filenumber + 1 def close( this ): #close all connections and sockets this.conn.close() this.sock.close() def process( this ): #this is the loop of the thread, it listens, receives, closes then repeats until entire program is closed while 1: this.bindsock() this.acceptsock() this.transfer() this.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] seeking help to a problem w/ sockets
"James Duffy" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] [snip] def close( this ): #close all connections and sockets this.conn.close() this.sock.close() def process( this ): #this is the loop of the thread, it listens, receives, closes then repeats until entire program is closed while 1: this.bindsock() this.acceptsock() this.transfer() this.close() There is no need to close the server socket after each connection. Try: def close( this ): #close all connections and sockets this.conn.close() def process( this ): this.bindsock() while 1: this.acceptsock() this.transfer() this.close() Also, take a look at the SocketServer libary. It handles multiple simultaneous connections and the details of setting up and tearing down connections: import threading import SocketServer class MyHandler(SocketServer.StreamRequestHandler): def handle(self): print 'Starting media transfer ' openfile="XMLrecieved"+str(self.server.filenumber)+".xml" f = open(openfile,"wb") while 1: data = self.request.recv(1024) if not data: break f.write(data) f.close() print "Got XML file:" + openfile print 'Closing media transfer' self.server.filenumber += 1 class MyServer(SocketServer.ThreadingTCPServer): filenumber = 1 class MyThread(threading.Thread): def run(self): listen= server = MyServer(('',listen),MyHandler) server.serve_forever() t=MyThread() t.setDaemon(True) t.start() --Mark ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] seeking help to a problem w/ sockets
How is the window being closed? By someone forcing it to close? Or terminating the process? If someone is just closing the window you can setup an atexit handler that will close the socket before it finishes. However, if the process is being terminated, then you will have to use one of the other options. If that's the only way you have to close the program then you will definitely need a flag of some sort. I've been known to play all sorts of tricks. Most nastiest of which includes creating a file in a particular directory that the program periodically polls. Don't do this. Use threads. Or a non-blocking keypress or something. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] seeking help to a problem w/ sockets
James Duffy wrote: > I have a problem w/ a file transfer receiver. They way it works is it > binds a port for incoming transfer , when the file transfer is complete. > It closes the connection and the socket, then loops back and restarts > the bind and listen. I have it set so that the socket is reuseable, > which is why this works. However, if the program that is using this > function is closed while listening, it appears that it does not > ”un-bind” because when the program is reopened and a listen attepted to > start I get a “port already in use” error. Only a reboot fixes this > issue. This code is imported into a main GUI script. We have it set to > execute some cleanup functions on exit, I need a function that can dig It should be noted that my socket, threading, and related gui skillz are lacking, so beware. IIUC, the usual cause for an 'address already in use' error is when the server closes it's end of the socket first, leaving it in a TIME_WAIT state. Presumably, this would be a feature of tcp, and the socket is released after some timeout period to be sure the client is no longer communicating. However setting the socket options as you have, with setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1), is the typically suggested workaround in this situation. Also, given that you need to reboot to clear the error, I suspect that your gui program isn't actually terminating -- but instead hanging on the Receiver thread, and thus holding on to the socket. You can, of course, confirm with netstat and/or by checking the process list. If you just want the thread to die when you close the gui app, then you could also try to setDaemon(True) on your Receiver class -- which should allow the program to exit, that is -- if there are no non-daemon threads remaining. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] seeking help to a problem w/ sockets
James Duffy wrote: I have a problem w/ a file transfer receiver. They way it works is it binds a port for incoming transfer , when the file transfer is complete. It closes the connection and the socket, then loops back and restarts the bind and listen. I have it set so that the socket is reuseable, which is why this works. However, if the program that is using this function is closed while listening, it appears that it does not ”un-bind” because when the program is reopened and a listen attepted to start I get a “port already in use” error. Only a reboot fixes this issue. This code is imported into a main GUI script. We have it set to execute some cleanup functions on exit, I need a function that can dig down to the thread the listener is running in, stop the listen and close the connection and socket. I basically need to get to the close function and then stop the while loop. The usual way to terminate a thread from another thread is to set a flag that the running thread checks periodically. Here is an example: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65448 This discussion may be helpful: http://groups.google.com/group/comp.lang.python/browse_thread/thread/7f116836f1fd2805/6adcc7e371238fdd?lnk=gst Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] seeking help to a problem w/ sockets
"James Duffy" <[EMAIL PROTECTED]> wrote works. However, if the program that is using this function is closed while listening, it appears that it does not "un-bind" because when the program is reopened and a listen attepted to start I get a "port already in use" error. Only a reboot fixes this issue. You don;t say what OS you are using but I have had this problem on *nix boxes where the socket/port is represented by a file in /tmp and you need to go in and kill the file manually(or by a script of course) before you can reopen the socket. If its not *nix then you may have to look at a similar fix on your OS - maybe in the registry of Windows for example. the listen and close the connection and socket. I basically need to get to the close function and then stop the while loop. Thanks in advance for any help anyone can give. My code for the listener class follows: I haven't read the code, sorry, but could you use a try/finally wrapper around your threads? Just a thought, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] seeking help to a problem w/ sockets
I have a problem w/ a file transfer receiver. They way it works is it binds a port for incoming transfer , when the file transfer is complete. It closes the connection and the socket, then loops back and restarts the bind and listen. I have it set so that the socket is reuseable, which is why this works. However, if the program that is using this function is closed while listening, it appears that it does not "un-bind" because when the program is reopened and a listen attepted to start I get a "port already in use" error. Only a reboot fixes this issue. This code is imported into a main GUI script. We have it set to execute some cleanup functions on exit, I need a function that can dig down to the thread the listener is running in, stop the listen and close the connection and socket. I basically need to get to the close function and then stop the while loop. Thanks in advance for any help anyone can give. My code for the listener class follows: class Reciever ( Thread ): # the reciever class for the test tool, runs as a separate thread from the main program def __init__( this ): Thread.__init__( this ) def run(this): this.process() def bindsock( this ): # create a new socket, bid the socket to the port, listen until connection recieved this.Lport=listen this.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) this.sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) this.sock.bind(('',this.Lport)) this.sock.listen(1) print "Listening on port " +str(this.Lport) def acceptsock( this ): #when connection is incoming, accept the connection this.conn, this.addr = this.sock.accept() print 'Got connection from', this.addr def transfer( this ): #when connection is full established, begin data download global filenumber print 'Starting media transfer ' openfile="XMLrecieved"+str(filenumber)+".xml" f = open(openfile,"wb") #create and open a new file for writing incoming data to while 1: data = this.conn.recv(1024) #check for incoming data if not data: break #if not present, break the loop f.write(data) #if data is present, write it to file and loop back f.close() #when loop is broken, close the file print "Got XML file:" + openfile print 'Closing media transfer' filenumber = filenumber + 1 def close( this ): #close all connections and sockets this.conn.close() this.sock.close() def process( this ): #this is the loop of the thread, it listens, receives, closes then repeats until entire program is closed while 1: this.bindsock() this.acceptsock() this.transfer() this.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor