🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
4 messages
4 total messages Started by Orlando Lopez Sat, 21 Jan 2006 02:45
Tryt to bind TClientSocket to specific LocalPort
#2825
Author: Orlando Lopez
Date: Sat, 21 Jan 2006 02:45
31 lines
1696 bytes
Hi to all,

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?


void __fastcall TForm1::ClientSocket1Lookup(TObject *Sender,
      TCustomWinSocket *Socket)
{
    TSockAddrIn *Addr;
    int Err;

    memset(Addr, 0, sizeof(Addr));
    Addr->sin_family = PF_INET;
//here : [C++ Error] E2034 Cannot convert 'unsigned long' to 'in_addr'
    Addr->sin_addr = INADDR_ANY;
    Addr->sin_port = htons(3033);

    if (bind(Socket->SocketHandle, Addr, sizeof(Addr)) != 0)
    {
        Err = WSAGetLastError;
        if (Err != WSAEWOULDBLOCK)
        {
            ShowMessage("Some Error ocurred, check");
        }
    }
}

Tnanks in advance
Re: Tryt to bind TClientSocket to specific LocalPort
#2826
Author: 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
Re: Tryt to bind TClientSocket to specific LocalPort
#2829
Author: Orlando Lopez
Date: Mon, 23 Jan 2006 00:16
9 lines
1190 bytes
Thanks a lot Gambit,
worked great.
I'm getting an error with this line:
>throw ESocketError(Rtlconsts_sWindowsSocketError,
>ARRAYOFCONST((SysErrorMessage(Err), Err, 'bind')));

but anyway, I will manage this error by other way.
Thanks alot again.
Re: Tryt to bind TClientSocket to specific LocalPort
#2831
Author: Remy Lebeau (TeamB)
Date: Mon, 23 Jan 2006 07:33
10 lines
1183 bytes
"Orlando Lopez" <hlopezrd@hotmail.com> wrote in message
news:43d42e51$1@newsgroups.borland.com...

> I'm getting an error with this line:

Please be more specific.  Also, which version of BCB are you using?


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads