Hello,
I have an application which frequently fails to start with the message:
"Could not bind socket. Address and port are already in use."
This happens (for no apparent reason) often enough that it is time for me to
start investigating it.
When I got to my computer this morning, my Anti-Virus program (AVG) had a
message up that said my computer needed to restart to complete the update
process. But I wanted to check email and a few other things before
rebooting, so one of the first things I did was run the application, and
BOOM, I get the error. The application had not been running for at least a
day, and no other server applications use the same port. I have checked
"netstat -a" for any indication that the port is in use, but am never able
to find anything. Whenever this happens, I am forced to reboot the compute
to clear the cause (whatever it is) so I can run the application.
What the heck is going on, and is there anything I can do to fix it? Or is
it the operating system's fault?
I am using a recent snapshot of Indy9 with BCB6 on XP sp2.
Can anyone offer a solution or a path toward a solution?
Thank you,
- Dennis
----- Original Message -----
From: "Dennis Jones"
Newsgroups: borland.public.cppbuilder.internet.socket
Sent: Tuesday, November 20, 2007 11:07 PM
Subject: Re: 'could not bind socket' error
>>> In this particular case, the port number was 5018.
>>
>> And port 5018 does not appear anywhere in netstat -a or -b for any IP
>> address or hostname while the error is occuring?
>
> Yes, that is correct. That's what's so frustrating about the whole thing.
> If netstat -a showed me that the port was in use, then at least I would
> know where to start looking. But since it doesn't, I don't know if the
> problem is due to my application (failing to close the socket correctly in
> some unusual circumstance), or the OS doing something funky, or if it is
> something else entirely.
>
> I guess all I can do now is wait until it happens again and then see if I
> can find some way to figure out what's causing it. Any suggestions on how
> to go about doing would be most welcome. In the meantime, do you know of
> any way to clear the socket so that the IP/port can be used without having
> to reboot the machine?
Okay, it happened again. My application failed to open a socket on port
5018. I immediately went to a command prompt and invoked "netstat -a".
Here is the output (some identifying address information has been blocked
out). You will note that there is no entry for port 5018 anywhere in the
list, and yet I get "Could not bind socket. Address and port are already in
use." I also checked all of the named ports to discver their numeric
equivelent. None of them are port 5018.
Active Connections
Proto Local Address Foreign Address State
TCP djones_xp:epmap djones_xp:0 LISTENING
TCP djones_xp:microsoft-ds djones_xp:0 LISTENING
TCP djones_xp:902 djones_xp:0 LISTENING
TCP djones_xp:912 djones_xp:0 LISTENING
TCP djones_xp:1030 djones_xp:0 LISTENING
TCP djones_xp:2401 djones_xp:0 LISTENING
TCP djones_xp:3389 djones_xp:0 LISTENING
TCP djones_xp:8222 djones_xp:0 LISTENING
TCP djones_xp:8333 djones_xp:0 LISTENING
TCP djones_xp:1066 djones_xp:0 LISTENING
TCP djones_xp:2402 djones_xp:0 LISTENING
TCP djones_xp:3690 djones_xp:0 LISTENING
TCP djones_xp:4564 localhost:4565 ESTABLISHED
TCP djones_xp:4565 localhost:4564 ESTABLISHED
TCP djones_xp:4566 localhost:4567 ESTABLISHED
TCP djones_xp:4567 localhost:4566 ESTABLISHED
TCP djones_xp:10025 djones_xp:0 LISTENING
TCP djones_xp:10110 djones_xp:0 LISTENING
TCP djones_xp:10110 localhost:1419 TIME_WAIT
TCP djones_xp:10110 localhost:1421 TIME_WAIT
TCP djones_xp:10110 localhost:1423 TIME_WAIT
TCP djones_xp:10110 localhost:1425 TIME_WAIT
TCP djones_xp:10110 localhost:1427 TIME_WAIT
TCP djones_xp:10110 localhost:1430 TIME_WAIT
TCP djones_xp:10110 localhost:1432 TIME_WAIT
TCP djones_xp:10110 localhost:1436 TIME_WAIT
TCP djones_xp:10110 localhost:1438 TIME_WAIT
TCP djones_xp:10110 localhost:1440 TIME_WAIT
TCP djones_xp:62332 djones_xp:0 LISTENING
TCP djones_xp:netbios-ssn djones_xp:0 LISTENING
TCP djones_xp:netbios-ssn 192.168.1.50:3572 ESTABLISHED
TCP djones_xp:1025 192.168.1.2:microsoft-ds ESTABLISHED
TCP djones_xp:1027 neutron:microsoft-ds ESTABLISHED
TCP djones_xp:1037 207.224.24.86:2108 ESTABLISHED
TCP djones_xp:1069 cs21.msg.dcn.yahoo.com:5050 ESTABLISHED
TCP djones_xp:1071 by1msg3145602.phx.gbl:1863 ESTABLISHED
TCP djones_xp:1073 205.188.9.108:5190 ESTABLISHED
TCP djones_xp:1076 oam-d19c.blue.aol.com:5190 ESTABLISHED
TCP djones_xp:1396 by2msg1262105.phx.gbl:1863 ESTABLISHED
TCP djones_xp:1420 incoming.verizon.net:pop3 TIME_WAIT
TCP djones_xp:1422
static-xx-xxx-xxx-xx.ptldor.fios.verizon.net:pop3 TIME_WAIT
TCP djones_xp:1424
static-xx-xxx-xxx-xx.ptldor.fios.verizon.net:pop3 TIME_WAIT
TCP djones_xp:1426
static-xx-xxx-xxx-xx.ptldor.fios.verizon.net:pop3 TIME_WAIT
TCP djones_xp:1428
static-xx-xxx-xxx-xx.ptldor.fios.verizon.net:pop3 TIME_WAIT
TCP djones_xp:1429 209.213.221.138:4050 TIME_WAIT
TCP djones_xp:1431 incoming.verizon.net:pop3 TIME_WAIT
TCP djones_xp:1437
static-xx-xxx-xxx-xx.ptldor.fios.verizon.net:pop3 TIME_WAIT
TCP djones_xp:1439
static-xx-xxx-xxx-xx.ptldor.fios.verizon.net:pop3 TIME_WAIT
TCP djones_xp:1441
static-xx-xxx-xxx-xx.ptldor.fios.verizon.net:pop3 TIME_WAIT
TCP djones_xp:2315 neutron:2324 ESTABLISHED
TCP djones_xp:3243 soft-gems.net:nntp ESTABLISHED
TCP djones_xp:3978 aqaserver.automatedqa.com:nntp ESTABLISHED
TCP djones_xp:4025 nexusdb.com:nntp ESTABLISHED
TCP djones_xp:4265 207.105.83.62:563 ESTABLISHED
TCP djones_xp:4605 qb-in-f99.google.com:http CLOSE_WAIT
TCP djones_xp:4686 192.168.1.2:5900 ESTABLISHED
UDP djones_xp:epmap *:*
UDP djones_xp:microsoft-ds *:*
UDP djones_xp:isakmp *:*
UDP djones_xp:1029 *:*
UDP djones_xp:1035 *:*
UDP djones_xp:1494 *:*
UDP djones_xp:2241 *:*
UDP djones_xp:3343 *:*
UDP djones_xp:3456 *:*
UDP djones_xp:4500 *:*
UDP djones_xp:5353 *:*
UDP djones_xp:ntp *:*
UDP djones_xp:1900 *:*
UDP djones_xp:2636 *:*
UDP djones_xp:4373 *:*
UDP djones_xp:ntp *:*
UDP djones_xp:netbios-ns *:*
UDP djones_xp:netbios-dgm *:*
UDP djones_xp:ntp *:*
UDP djones_xp:1900 *:*
- Dennis
Know what, you are right....
:)
Cheers!
Also isn't it a bummer Uncle Microsoft removed
the sysInternals Code, just when was getting
interested in it.
Also I did save me some code from the old sites
and IMHO I think some of their code contributed
to rootkit development.
Remy Lebeau (TeamB) wrote:
> "Colin B Maharaj" wrote in message
> news:47443185@newsgroups.borland.com...
>
>> Also, under Win 2000, you cannot get the
>> application names
>
> TCPView from SysInternals
> (http://www.microsoft.com/technet/sysinternals/Networking/TcpView.mspx) is
> able to do that (even before Microsoft took over SysInternals), and can do
> so all the way back to NT4. So it is technically possible to do.
>
>
> Gambit
>
>
"Colin B Maharaj" wrote in message
news:47443185@newsgroups.borland.com...
> Also, under Win 2000, you cannot get the
> application names
TCPView from SysInternals
(http://www.microsoft.com/technet/sysinternals/Networking/TcpView.mspx) is
able to do that (even before Microsoft took over SysInternals), and can do
so all the way back to NT4. So it is technically possible to do.
Gambit
"Dennis Jones" wrote in message
news:47446f97$1@newsgroups.borland.com...
> Will that work even if the socket had been closed incorrectly/abnormally?
I don't know. I never tried reusing a socket that was in an incorrect state
before.
Gambit
"Remy Lebeau (TeamB)" wrote in message
news:474465f6$1@newsgroups.borland.com...
>
> "Dennis Jones" wrote in message
> news:4743d916$1@newsgroups.borland.com...
>
>> do you know of any way to clear the socket
>
> You can't. It is under the OS's control.
>
>> so that the IP/port can be used without having to reboot the machine?
>
> I answered that several replies ago - set the server's ReuseSocket
> property to rsTrue.
Oh, yes, I saw that. Will that work even if the socket had been closed
incorrectly/abnormally?
- Dennis
"Dennis Jones" wrote in message
news:4743d916$1@newsgroups.borland.com...
> do you know of any way to clear the socket
You can't. It is under the OS's control.
> so that the IP/port can be used without having to reboot the machine?
I answered that several replies ago - set the server's ReuseSocket property
to rsTrue.
Gambit
Hi Dennis, I have written this application that
needs to be launched, in some cases 10 or more
times. Each instance has a Web Server and obviously
needs to be assigned a unique port.
I have taken some code from a Code Project
article (i think) and adapted it for C++ Builder 6.
What this code does is enumerates all networking
applications and what port they are using and how.
What I do with it is auto assign ports to my application
instances using ports NOT in the list.
Also, under Win 2000, you cannot get the application
names, however 2003 Server and Win XP allows you
to see what application is using what port (and how).
NOT SUPPORTED IN 98/95.
Check the Attachments group.
Here is a code sample of a grid and edit box
on a form.
//-------------------------------------------------
// form with a StringGrid (renamed Grid1)
// and an Edit box
//-------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TNetstatX * NetstatX = new TNetstatX;
int n=0;
Grid1->Cells[n++][0] = "Type";
Grid1->Cells[n++][0] = "State";
Grid1->Cells[n++][0] = "Local Address";
Grid1->Cells[n++][0] = "Local Port";
Grid1->Cells[n++][0] = "Remote Address";
Grid1->Cells[n++][0] = "Remote Port";
Grid1->Cells[n++][0] = "pid";
Grid1->Cells[n++][0] = "Process";
NetstatX->Enumerate();
Grid1->RowCount = NetstatX->ENCPQty+1;
for (int i=0; iENCPQty; i++)
{
int n=0;
Grid1->Cells[n++][i+1] = NetstatX->ShowIfTCP(NetstatX->ENCP[i].IsTCP);
Grid1->Cells[n++][i+1] =
NetstatX->ShowTCPState(NetstatX->ENCP[i].State);
Grid1->Cells[n++][i+1] = NetstatX->ENCP[i].LocalAddr;
Grid1->Cells[n++][i+1] = NetstatX->ENCP[i].LocalPort;
Grid1->Cells[n++][i+1] = NetstatX->ENCP[i].RemoteAddr;
Grid1->Cells[n++][i+1] = NetstatX->ENCP[i].RemotePort;
Grid1->Cells[n++][i+1] = NetstatX->ENCP[i].pid;
Grid1->Cells[n++][i+1] = NetstatX->ENCP[i].ProcessName;
}
Edit1->Text = NetstatX->UsedTCPPorts();
delete NetstatX;
}
Dennis Jones wrote:
> "Dennis Jones" wrote in message
> news:47431d9f$1@newsgroups.borland.com...
>> Hello,
>>
>> I have an application which frequently fails to start with the message:
>>
>> "Could not bind socket. Address and port are already in use."
>>
>> This happens (for no apparent reason) often enough that it is time for me
>> to start investigating it.
>
> If it helps to see some code, here is the code that I use to start the
> server:
>
> void TMyServer::Start()
> {
> if ( !TCPServer->Active )
> {
> TCPServer->ThreadClass = __classid(TMyPeerThread);
> TCPServer->OnExecute = TCPServerExecute;
> TCPServer->OnConnect = TCPClientConnect;
> TCPServer->OnDisconnect = TCPClientDisconnect;
>
> TCPServer->DefaultPort = FPort;
>
> // Set up the bindings:
> TCPServer->Bindings->Clear();
> TIdSocketHandle *NewBinding = TCPServer->Bindings->Add();
> NewBinding->IP = FHostAddress;
>
> AnsiString HostAddress( "All Adapters" );
> if ( !FHostAddress.IsEmpty() )
> {
> HostAddress = FHostAddress;
> }
>
> LogMsg( AnsiString().sprintf( "Starting server on <%s:%d>",
> HostAddress.c_str(), FPort ) );
>
> TCPServer->Active = true;
>
> LogMsg( AnsiString().sprintf( "Started server on <%s:%d>",
> HostAddress.c_str(), FPort ) );
> }
> }
>
> - Dennis
>
>
"Remy Lebeau (TeamB)" wrote in message
news:4743c58d$1@newsgroups.borland.com...
>
> "Dennis Jones" wrote in message
> news:474384f3$1@newsgroups.borland.com...
>
>> The address is "" (an empty string).
>
> That will bind the server to all available IP addresses on the machine.
Yes, because that's exactly what I want. The application allows the user to
choose from one or more specific adapters (if desired), or "Any Available"
adapter, in which case "" (empty string) is used to allow client connections
from any adapter, regardless of its IP address.
>> In this particular case, the port number was 5018.
>
> And port 5018 does not appear anywhere in netstat -a or -b for any IP
> address or hostname while the error is occuring?
Yes, that is correct. That's what's so frustrating about the whole thing.
If netstat -a showed me that the port was in use, then at least I would know
where to start looking. But since it doesn't, I don't know if the problem
is due to my application (failing to close the socket correctly in some
unusual circumstance), or the OS doing something funky, or if it is
something else entirely.
It seems reasonable that it could be caused by me killing the application
since I do that on occasion via the debugger, but the error usually occurs
when there has been such a long period of time since I last ran the
application, that any memory of what I was doing at the time (or of how I
closed it) has long since been forgotten.
I guess all I can do now is wait until it happens again and then see if I
can find some way to figure out what's causing it. Any suggestions on how
to go about doing would be most welcome. In the meantime, do you know of
any way to clear the socket so that the IP/port can be used without having
to reboot the machine?
Thanks for your time, Remy.
- Dennis
"Dennis Jones" wrote in message
news:474384f3$1@newsgroups.borland.com...
> The address is "" (an empty string).
That will bind the server to all available IP addresses on the machine.
> In this particular case, the port number was 5018.
And port 5018 does not appear anywhere in netstat -a or -b for any IP
address or hostname while the error is occuring?
Gambit