473,563 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting socket.bind() exception, but no actual error

I've got a problem that I don't see how to program around. A socket server
that was running fine, today started getting an exception from the bind()
call (errno 22, Invalid argument) and yet the bind had actually worked.

This is apparent from this code: it prints the error message, but then
breaks out of the loop and the listen() and accept() calls work normally.

How can one handle a function that is both working and raising an
exception?

sockobj=socket( AF_INET,SOCK_ST REAM)
while True:
try:
sockobj.bind((' ',4321))
except socketerror,e:
print e
if str(e).find('in use') == -1:
break
print '.'
time.sleep(1)
sockobj.listen( 5)
Jul 18 '05 #1
6 10141
Clarence Gardner wrote:
I've got a problem that I don't see how to program around. A socket server
that was running fine, today started getting an exception from the bind()
call (errno 22, Invalid argument) and yet the bind had actually worked.

This is apparent from this code: it prints the error message, but then
breaks out of the loop and the listen() and accept() calls work normally.

How can one handle a function that is both working and raising an
exception?

sockobj=socket( AF_INET,SOCK_ST REAM)
while True:
try:
sockobj.bind((' ',4321))
except socketerror,e:
print e
if str(e).find('in use') == -1:
break
print '.'
time.sleep(1)
sockobj.listen( 5)


Is it possible you already have a process bound to that port, and that
process is handling the incoming connections?

Otherwise it sounds a little bizarre. In fact even my own attempt at
explaining doesn't ring true because on Win2k, at least, a second bind
will raise (10048, 'Address already in use'), and I get a similar error
on Linux. Rats.

I take it this code has been copied and pasted? For example, if the host
were a single space rather than an empty string /that/ might cause this
problem. Sorry I can't be more help.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Jul 18 '05 #2
cl******@silcom .com (Clarence Gardner) writes:
And the whole point of the test in the exception handling suite (checking
for "in use") is to repeat the bind until that's not the case. This is, of
course, in a program in development which sometimes is not able to be
restarted right away after a problem.


Why not? Because you get "Address already in use" exceptions due
to old connections still being in the TIME_WAIT state? If so, are
you aware that server processes should usually set the SO_REUSEADDR
socket option before calling bind()? Or is there some other reason
that bind() fails?

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 18 '05 #3
Michael Fuhr wrote:
cl******@silcom .com (Clarence Gardner) writes:

And the whole point of the test in the exception handling suite (checking
for "in use") is to repeat the bind until that's not the case. This is, of
course, in a program in development which sometimes is not able to be
restarted right away after a problem.

Why not? Because you get "Address already in use" exceptions due
to old connections still being in the TIME_WAIT state? If so, are
you aware that server processes should usually set the SO_REUSEADDR
socket option before calling bind()? Or is there some other reason
that bind() fails?


There is some other reason: the exception argument is (errno 22, Invalid
argument), which is clearly not "Address in use".

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Jul 18 '05 #4
Steve Holden <st***@holdenwe b.com> writes:
Michael Fuhr wrote:
cl******@silcom .com (Clarence Gardner) writes:
And the whole point of the test in the exception handling suite (checking
for "in use") is to repeat the bind until that's not the case. This is, of
course, in a program in development which sometimes is not able to be
restarted right away after a problem.


Why not? Because you get "Address already in use" exceptions due
to old connections still being in the TIME_WAIT state? If so, are
you aware that server processes should usually set the SO_REUSEADDR
socket option before calling bind()? Or is there some other reason
that bind() fails?


There is some other reason: the exception argument is (errno 22, Invalid
argument), which is clearly not "Address in use".


The retry loop likely isn't solving this problem but is rather
causing it. The code originally posted was:

sockobj=socket( AF_INET,SOCK_ST REAM)
while True:
try:
sockobj.bind((' ',4321))
except socketerror,e:
print e
if str(e).find('in use') == -1:
break
print '.'
time.sleep(1)
sockobj.listen( 5)

Think about what happens if bind() succeeds: the code never breaks
out of the while loop, so it attempts to call bind() again. On
most systems that will cause the second call to bind() to fail with
EINVAL, or "Invalid argument". You could break out of the loop
when bind() succeeds, but the loop should be unnecessary if you set
the SO_REUSEADDR socket option before calling bind(), like this:

sockobj.setsock opt(SOL_SOCKET, SO_REUSEADDR, 1)

Any decent network programming book (e.g., _UNIX Network Programming_,
Vol 1, by W. Richard Stevens) will explain that servers should set
SO_REUSEADDR before calling bind().

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 18 '05 #5
mf***@fuhr.org (Michael Fuhr) wrote in message news:<41******* ***@omega.dimen sional.com>...
? Because you get "Address already in use" exceptions due
to old connections still being in the TIME_WAIT state? If so, are
you aware that server processes should usually set the SO_REUSEADDR
socket option before calling bind()? Or is there some other reason
that bind() fails?


No, that's the only issue. I thought using that option was frowned upon,
but it's also been a couple of years since I needed to think about such
things, and I was too lazy to look it up for now.
Jul 18 '05 #6
cl******@silcom .com (Clarence Gardner) writes:
mf***@fuhr.org (Michael Fuhr) wrote in message news:<41******* ***@omega.dimen sional.com>...
? Because you get "Address already in use" exceptions due
to old connections still being in the TIME_WAIT state? If so, are
you aware that server processes should usually set the SO_REUSEADDR
socket option before calling bind()? Or is there some other reason
that bind() fails?


No, that's the only issue. I thought using that option was frowned upon,
but it's also been a couple of years since I needed to think about such
things, and I was too lazy to look it up for now.


SO_REUSEADDR is hardly frowned upon -- quite the contrary: it's
recommended. See for example the socket bible, _UNIX Network
Programming_, Vol 1, by W. Richard Stevens. On p 207 (2nd edition),
in the summary for the Socket Options chapter, Stevens says:

The most commonly used options that we might encounter are
SO_KEEPALIVE, SO_RCVBUF, SO_SNDBUF, and SO_REUSEADDR. The
latter should always be set for a TCP server before it calls
bind.

Earlier in the chapter, on pp 194-197, Stevens discusses SO_REUSEADDR
in detail and says that "_All_ TCP servers should specify this
socket option to allow the server to be restarted in this situation"
(emphasis his; page numbers may differ in the 3rd edition).

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 18 '05 #7

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

Similar topics

5
4430
by: eeh | last post by:
Hi, I got bind exception (Jave.ent.BindException: Cannot assign requested address: JVM_BIND) when the following statement is executed. ss=new ServerSocket(0x55,0,InetAddress.getByName("192.168.0.8")); The host 192.168.0.8 is on and can be pinged. Could anyone tell me why?
3
3230
by: J. Muenchbourg | last post by:
I'm getting an Exception occured error on line 4 (For i =1...) <% Function ProperCase(strIn) strOut = "" boolUp = True For i = 1 To Len(strIn) c = Mid(strIn, i, 1) if c = " " or c = "'" or c = "-" then strOut = strOut & c boolUp = True
4
2743
by: 0k | last post by:
Hi everyone, I am trying to write a small app that sends multicast udp packets using a socket object. I have more than one NIC on my PC and the following code works OK only if I disable all the NICs but the one i want to use. Of course i tried to use Socket.Bind method, but even if i use it to bind to the correct NIC (I also verify using...
1
1957
by: manish | last post by:
Hi, I am a fresher in the programming field i.e although I have done programming at the basic level but at professional level I am very new and I am facing many problems. These probllems are not taughtand I am not getting any Refrences to cope with them. ********Setting in VC++ 6.0 I don'know to apply setting for various/different ...
1
1439
by: ramonred | last post by:
Hi, I am following along an example from the book <b>Beginning Visual Web Programming in C#</b> My environment is this: IIS 5.1 on XP Pro, I have the checkbox 'use windows integrated authentication' unchecked, and I am allowing anonymous access. Prior to building the example that is giving the error, the app would ask you to login if...
6
2329
by: Paul Steele | last post by:
I often use the following code to check if a program is already running: if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) ... This works find on all of my systems, but a student using one of my programs reported that he was getting an "Unhandled Exception" trying to load my program. This is even...
5
2691
by: Peter Steele | last post by:
We have an application that when it runs in the IDE in debug mode an unhandled exception is occurring in a system header file associated with STL stirngs. The actual statement that crashes is return ::memcmp(_First1, _First2, _Count); On inspecting these variables, the strings are in fact equal when the exception occurs and _Count is the...
0
1045
by: 0k | last post by:
Hi everyone, I am trying to write a small app that sends multicast udp packets using a socket object. I have more than one NIC on my PC and the following code works OK only if I disable all the NICs but the one i want to use. Of course i tried to use Socket.Bind method, but even if i use it to bind to the correct NIC (I also verify using...
5
11797
by: SamirM | last post by:
i am new to java. i am having trouble running some of my codes. Can someone tell me why i am getting "I/O Exception while Reading, Connection refused", message.?
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
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. ...
0
7950
tracyyun
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...
0
6255
agi2029
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...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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 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.