Mailing List Archive

problem with SocketServer -- address already in use
I'm trying to use SocketServer.py to write a TCP server. I ran
my program with ``python serv.py'', which went into an infinite
loop (which is what it should do). I exited my program with ^Z,
and tried to start it up again. I got an error message:

socket.error: (98, 'Address already in use')

which I presume is because something thinks the old invokation of
my program is still using port 7070.

How do I clear the port, before I start up my server?

--
Phil Hunt....philh@vision25.demon.co.uk
problem with SocketServer -- address already in use [ In reply to ]
On Tue, 13 Jul 99 21:05:28 GMT, Phil Hunt wrote:
>I'm trying to use SocketServer.py to write a TCP server. I ran
>my program with ``python serv.py'', which went into an infinite
>loop (which is what it should do). I exited my program with ^Z,
_____________________________________________________________^^
>and tried to start it up again. I got an error message:
>
>socket.error: (98, 'Address already in use')

ctrl-Z suspends your server, so the port is still occupied.
Use ctrl-C to interrupt it and wait, or use setsockopt as Gordon
suggests and ctrl-C. In any case, you can't use that port again
with a suspened server.

--C

--
Constantinos A. Kotsokalis || C.Kotsokalis@ece.ntua.gr
National Technical University of Athens - GREECE
Electrical and Computer Engineering Department
"Bus error -- driver executed."
problem with SocketServer -- address already in use [ In reply to ]
Phil Hunt wrote:
>
> I'm trying to use SocketServer.py to write a TCP server. I ran
> my program with ``python serv.py'', which went into an infinite
> loop (which is what it should do). I exited my program with ^Z,
> and tried to start it up again. I got an error message:
>
> socket.error: (98, 'Address already in use')
>
> which I presume is because something thinks the old invokation of
> my program is still using port 7070.
>
> How do I clear the port, before I start up my server?

The sockets stays allocated and in use for some timeout period after you
close it.
I solved this one by setting the SO_REUSEADDR option thru overridding
server_bind:

def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
SocketServer.TCPServer.server_bind(self)


--
- aew
problem with SocketServer -- address already in use [ In reply to ]
Phil Hunt writes:
> I'm trying to use SocketServer.py to write a TCP server. I ran
> my program with ``python serv.py'', which went into an infinite
> loop (which is what it should do). I exited my program with ^Z,
> and tried to start it up again. I got an error message:
>
> socket.error: (98, 'Address already in use')
>
> which I presume is because something thinks the old invokation of
> my program is still using port 7070.

Did you actually kill the process after doing ^Z? ^Z just suspends a process.

> How do I clear the port, before I start up my server?

You don't. The kernel clears the port, after a certain period of time
has elapsed. However, if the old process really is gone, and you want
to bind to the same address, without waiting for the kernel, use the
SO_REUSEADDR flag to setsockopt:

self.socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)

This must be done after a socket is created and before you call listen
or bind.

I don't see an easy way to do this within the framework of SocketServer.py

It might be nice if there were an optional flag in SocketServer to
allow the user to specify this behaviour.
problem with SocketServer -- address already in use [ In reply to ]
Phil Hunt wrote:

> I'm trying to use SocketServer.py to write a TCP server. I ran
> my program with ``python serv.py'', which went into an infinite loop
> (which is what it should do). I exited my program with ^Z, and tried
> to start it up again. I got an error message:
>
> socket.error: (98, 'Address already in use')
>
> which I presume is because something thinks the old invokation of my
> program is still using port 7070.
>
> How do I clear the port, before I start up my server?

Three non-exclusive optons:

1) Wait. It will eventually go away.

2) Use a try:... finally: ... and close the bound socket in the
finally clause.

3) Before binding the socket, use setsockopt(SOL_SOCKET,
SO_REUSEADDR, 1)

Once it has happened, (1) is your only viable alternative.

- Gordon
problem with SocketServer -- address already in use [ In reply to ]
Phil Hunt <philh@vision25.demon.co.uk> wrote:
> I'm trying to use SocketServer.py to write a TCP server. I ran
> my program with ``python serv.py'', which went into an infinite
> loop (which is what it should do). I exited my program with ^Z,
> and tried to start it up again. I got an error message:
>
> socket.error: (98, 'Address already in use')
>
> which I presume is because something thinks the old invokation of
> my program is still using port 7070.
>
> How do I clear the port, before I start up my server?

use setsockopt to enable local address reuse; see

http://www.deja.com/getdoc.xp?AN=484041505&fmt=text

for an example.

</F>