🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #721

Re: 'could not bind socket' error

#721
From: Remy Lebeau (TeamB)
Date: Tue, 20 Nov 2007 21:59
57 lines
3527 bytes
"Dennis Jones" <nospam@nospam.com> wrote in message 
news:47434723$1@newsgroups.borland.com...

> Right, I am pretty sure I understand what the error means,
> I just don't understand *why* I am getting it.

Well, there is only one reason *why* it happens - the IP/Port is already in 
use somewhere else on the machine :-)  *Why* the IP/Port is already in use 
is a different issue.

> Yes, that is correct, it is a server socket.

Ok.  Then you (or someone else) opened that IP/Port and didn't close it 
before you tried to activate a new server on it.

> there are no other instances of the application running, which
> is why the I'm asking the question.  (the application uses a
> mutex to prevent multiple instances)

That doesn't negate the possibility of the IP/Port being in use by a 
previous instance, though.  Maybe the previous instance crashed.  Maybe it 
didn't close its socket correctly.

> If I knew the answer to that, I wouldn't be asking the question
> in the first place!!

They way you worded your question did not explain why you couldn't find the 
information.

> "netstat -a" does not show any application instances that
> reference the port number I am trying to open in my server.

No, but "netstat -b" will.  Both "-a" and "-b" show socket statuses, though. 
If you start your server on port 12345, and you see Port 12345 listed, then 
you know that socket is in use, and what state it is in.

Something else to keep in mind - when a socket is closed, it is not FULLY 
closed right away.  The OS keeps it alive for an extra period of time in 
order to ensure any pending data gets handled at the network layer.  netstat 
will show such sockets in the TIME_WAIT state.  You can't bind a new socket 
to the same IP/Port until the previous socket is completely closed and 
released (which will take a few minutes), unless you use setsockopt() to 
enable the SO_REUSEADDR flag on the new socket before binding it. 
TIdTCPServer has a ReuseSocket property that you can set to rsTrue for that, 
if you are trying to reactivate your server too quickly.

> So if netstat does *not* show the port is in use

It will, otherwise you would not be getting the error.

> why would the server socket complain that it *is* in use?

It wouldn't, if it is not in netstat's listing.


Gambit
Technical Details
Message-ID: <474358f3@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!198.186.194.251.MISMATCH!transit4.readnews.com!news-out.readnews.com!news-xxxfer.readnews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <47431d9f$1@newsgroups.borland.com> <474342ce@newsgroups.borland.com> <47434723$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,128d041863311899
X-Google-Attributes: gid11508e,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <47431d9f$1@newsgroups.borland.com> <474342ce@newsgroups.borland.com> <47434723$1@newsgroups.borland.com>
Subject: Re: 'could not bind socket' error
Date: Tue, 20 Nov 2007 13:59:10 -0800
Lines: 59
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Response
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
NNTP-Posting-Host: 207.178.157.90
Message-ID: <474358f3@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1195596019 207.178.157.90 (20 Nov 2007 14:00:19 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!198.186.194.251.MISMATCH!transit4.readnews.com!news-out.readnews.com!news-xxxfer.readnews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail