473,695 Members | 2,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I have a socket server like below which I want to exit when
it's out of data. If I interrupt the client, I'll get a
broken pipe on the server side, and after a Ctrl-C, I can
restart the server again, but if I let it run out of data,
and exit via handle_error as can be seen below, I will get
a socket.error: (98, 'Address already in use') when I try
to restart the server. (Unless I wait a minute or so, or
use another port.) I feel like I'm waving a dead chicken
here. I can't seem to find any proper description on how to
handle this in the docs for SocketServer or socket, and my
futile experiments seem to lead nowhere... Neither Nutshell
nor the Essential Reference helped me solve this.

I'm using Python 2.2.3 on Linux. Why doesn't my socket
get released by the OS when I exit via my handle_error?

class MyRequestHandle r(SocketServer. BaseRequestHand ler):
def handle(self):
while True:
except [snip]:
# Done!
# Uncommenting the next line makes no difference
# self.request.cl ose()

class ExitableSocketS erver(SocketSer ver.TCPServer):
def handle_error(se lf, request, client_address) :
exc_info = sys.exc_info()
if exc_info and isinstance(exc_ info[1], SystemExit):
request.shutdow n(2)
del request
self.socket.shu tdown(2)
self.socket.clo se()
del self.socket
SocketServer.TC PServer.handle_ error(self, request, client_address)


server = ExitableSocketS erver(('', port), MyRequestHandle r)
server.serve_fo rever()
Jul 19 '05 #1
3 13909
On Mon, 30 May 2005 20:04:02 +0200, Magnus Lycka <ly***@carmen.s e>
declaimed the following in comp.lang.pytho n:

I'm using Python 2.2.3 on Linux. Why doesn't my socket
get released by the OS when I exit via my handle_error?
Not a Python problem...

Normal socket behavior IS to lock out for some period of time.

Check the options available when creating the socket. (I'm not
heavily familiar with socket programming, hence the vagueness) Some
option for "Reuse Address"...

SO_REUSEADDR is the constant in socket.h... And appears to be
visible in socket.py

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 19 '05 #2
Magnus Lyckå wrote:
Why doesn't my socket
get released by the OS when I exit via my handle_error?

Hi Magnus,

I wrote about this at

The reason for it is described at

You can set the class variable "allow_reuse_ad dress = True" in
your derived ExitableSocketS erver to get the behaviour you
expect, at the expense of some problems mentioned in the
above URL.

da***@dalkescie ntific.com

Jul 19 '05 #3
Thank's a lot Andrew and Dennis.

Andrew Dalke wrote:
You can set the class variable "allow_reuse_ad dress = True" in
your derived ExitableSocketS erver to get the behaviour you
expect, at the expense of some problems mentioned in the
above URL.

Ok. I added a STOP message to my protocol, so now the client
shuts down the socket first (when it has read the STOP), and
then it seems to work much better. What I did, in case the
client gets behind, was this in my handle method: I send a STOP
message repeatedly with a delay. When the client gets the first
one, it will close the socket, and I will get an exception the
next time I try to send. This seems to work well, without
(I hope) introducing any new potential problems.

delay = 1
print "No more data to serve. Send STOP message."
print "Waiting for client to close socket."
while 1:
self.request.se nd("STOP\n")
time.sleep(dela y)
print '.'
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Johannes Eble | last post by:
Hello Python community, I am trying the echo-client and echo-server examples in Chapter 10, "Programming Python" by Mark Lutz. It is probably the most simple sockets sample: A socket server just echoing the socket clients' requests. The program works so far. I first start the server in one dos box, then the client in another dos box on my machine.
by: Rune | last post by:
Hi, I've written a very simple 'kill-server' to help me shut down processes through Telnet or HTTP. The kill-server is a function and is launched as a thread. I use the module socket.py on Python v2.3 (Windows) I can use telnet host:port and enter the secret killword or use a broser with http://host:port/secret_killword The 'kill-server' validates the secret_killword and writes a file
by: Ajay | last post by:
hi! my application is throwing a bad socket error raise error(EBADF, 'Bad file descriptor') socket.error: (9, 'Bad file descriptor') basically i have the following interchange application service <server1> <client1>
by: Eric.Hillmuth | last post by:
I'm just getting started with postgresql and things are going pretty well. However, I seem to be having trouble connecting to my database via TCP/IP. I'm assuming that my problems are related to the message I get at startup (using "postmaster -D data - i"). The postmaster starts but produces this message: LOG: could not bind IPv4 socket: Address already in use HINT: is another postmaster already running on port 5432. If not, wait a few...
by: Tor Erik | last post by:
Hi, The reason is that my application does about 16 connects and data transfers per second, to the same 16 remote hosts. After approx 200 secs there are 4000 sockets waiting to be garbage collected by the OS. At this point is seems that connect loops and starts using the same local addresses it used 4000 connections ago, resulting in an "Address already in use" exception. A possible solution to this would be to keep the connection to...
by: palmem | last post by:
I am trying to write a simple FTP server in order to learn about sockets This is my first time trying sockets This code should take a connection on port 8110, dump it to a client "thread" (not a thread yet), print "Test\n" to the thread, and close everything. It fails on creating the client thread with error 106: 'Transport endpoint is already connected'
by: sivasankarmathuraju | last post by:
Hi! I am using: Java 2 SDK 1.4.0 Standard Edition on Windows XP I have one java client application and one gateway that is in C language runing on the same machine. The server creates a ServerSocket and starts lisening for connections The client creates a socket to the server. while i running the client(java) program it gives the following error message.
by: ntech | last post by:
hello, i got a problem while restarting postgresql, i removed postmaste.pid and even the temporary sock file and while restarting postgres, i got the following error: LOG: could not bind IPv6 socket: Address already in use HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. LOG: could not bind IPv4 socket: Address already in use HINT: Is another postmaster already running on port 5432? If...
by: khalid galal | last post by:
Hi, i am using NetBeans IDE 6.0.1 and i created a server project and a client project, first i run the server that listens to the port "8221" and waits for clients accessing that port, and when i run the client that accesses the server at port "8221" the IDE throws this exception "Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind at java.net.PlainSocketImpl.socketBind(Native Method)" By the way, i...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.