February 22nd, 2005, 11:10 AM
sockets & address already in use error
Fairly new to Python - got a module running a server on a designated port on the localhost. However, when I stop the server and try to run it again I get the following error:
socket.error: (98, 'Address already in use')
Unless, I choose a different port the error does not go away. My code is:
ip = 'localhost'
port = 3458
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
channel,details = mySocket.accept()
print 'Connection established with', details
channel.send('Connection established to server\n')
channel.send('Logging please wait...\n')
Any ideas please?
February 22nd, 2005, 11:33 AM
Don't assume garbage collection will tidy up for you...
port 3458 is open.
Don't forget code tags.
I would think a try/finally block would help - so whatever the reason you can always close the server port.
February 22nd, 2005, 11:55 PM
Well, usually that error means that port is still in use. One way to prevent this is to close your sockets after using them with the close(). The only way (In my mind) to fix that is just wait for the connection to timeout or change to a different port.
February 23rd, 2005, 11:59 AM
I have written some server's in the past, and Nick is correct.
If you do not explicitly tell the socket to close on termination of the server, you will need to wait for the OS to time it out.
Do some googling on daemonizing a python script, and you can find some useful information on how to react to a SIGTERM, and you can make sure the socket is closed when you exit.
February 24th, 2005, 03:22 AM
Sort of what I said but more clearly put.
Originally Posted by djjhouse