🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Tryt to bind TClientSocket to specific LocalPort

#2826
From: Remy Lebeau (TeamB)
Date: Sat, 21 Jan 2006 07:40
68 lines
3206 bytes
"Orlando Lopez" <hlopezrd@hotmail.com> wrote in message
news:43d1ae4b$1@newsgroups.borland.com...

> I'm trying to bind specific port (3033) to my TClientSocket.
> I'm trying to do this (Thanks gambit, this code was original
> in delphi), but I;m getting some errors, why?

Your code is wrong.  See below.

>     TSockAddrIn *Addr;

You have declared an unintialized pointer that you never assign to any
memory address.

>     memset(Addr, 0, sizeof(Addr));

Because your pointer is uninitialized, you are accessing random memory.

>     Addr->sin_family = PF_INET;
> //here : [C++ Error] E2034 Cannot convert 'unsigned long' to 'in_addr'
>     Addr->sin_addr = INADDR_ANY;

The sin_addr member is itself a structure of type in_addr.  You need to use
the s_addr macro to assign the proper member of the in_addr structure.

>     if (bind(Socket->SocketHandle, Addr, sizeof(Addr)) != 0)

The SocketHandle property is declared as an 'int'.  You need to cast it to a
SOCKET when passing its value to any socket API function, such as bind().

>         Err = WSAGetLastError;
>         if (Err != WSAEWOULDBLOCK)

bind() is never an asynchronous operation.  WSAGetLastError() will never
return WSAEWOULDBLOCK for bind().

>            ShowMessage("Some Error ocurred, check");

I suggest that you throw an exception instead of displaying a popup message.
If you do not, the rest of your code will never know whether the bind()
succeeded or failed.  If you throw an exception, the socket will be
automatically disconnected, and then your code that is opening the socket
can react to the error accordingly.

With that said, use this code:

    #include <RtlConsts.hpp>

    void __fastcall TForm1::ClientSocket1Lookup(TObject *Sender,
TCustomWinSocket *Socket)
    {
        sockaddr_in Addr = {0};
        Addr.sin_family = PF_INET;
        Addr.sin_addr.s_addr = INADDR_ANY;
        Addr.sin_port = htons(3033);

        if( ::bind((SOCKET)Socket->SocketHandle, (sockaddr*)&Addr,
sizeof(Addr)) == SOCKET_ERROR )
        {
            int Err = WSAGetLastError();
            throw ESocketError(Rtlconsts_sWindowsSocketError,
ARRAYOFCONST((SysErrorMessage(Err), Err, 'bind')));
        }
    }


Gambit
Technical Details
Message-ID: <43d1e633$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!newshub.sdsu.edu!pln-w!lotsanews.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <43d1ae4b$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,98a68f0c9b203d3
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <43d1ae4b$1@newsgroups.borland.com>
Subject: Re: Tryt to bind TClientSocket to specific LocalPort
Date: Fri, 20 Jan 2006 23:40:30 -0800
Lines: 70
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <43d1e633$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1137829427 68.69.17.41 (20 Jan 2006 23:43:47 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!newshub.sdsu.edu!pln-w!lotsanews.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail