Article View: borland.public.cppbuilder.internet.socket
Article #728Re: 'could not bind socket' error
From: Colin B Maharaj
Date: Wed, 21 Nov 2007 13:24
Date: Wed, 21 Nov 2007 13:24
115 lines
4705 bytes
4705 bytes
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; i<NetstatX->ENCPQty; 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" <nospam@nospam.com> 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
>
>
Technical Details
Message-ID:
<47443185@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<47431d9f$1@newsgroups.borland.com> <4743264f@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 Date: Wed, 21 Nov 2007 09:24:20 -0400 From: Colin B Maharaj <noreply@myhost.com> User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.internet.socket Subject: Re: 'could not bind socket' error References: <47431d9f$1@newsgroups.borland.com> <4743264f@newsgroups.borland.com> In-Reply-To: <4743264f@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 071120-0, 11/20/2007), Outbound message X-Antivirus-Status: Clean NNTP-Posting-Host: 201.238.107.132 Message-ID: <47443185@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1195651461 201.238.107.132 (21 Nov 2007 05:24:21 -0700) Lines: 115 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail