🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: BDS2006 requires WSAStartup be called...

#2286
From: Remy Lebeau (TeamB)
Date: Tue, 06 Jun 2006 16:42
71 lines
3336 bytes
"Mark Jacobs" <http://www.jacobsm.com/mjmsg?Borland%20Newsgroup> wrote in
message news:44855a92$1@newsgroups.borland.com...

>   mhstj=gethostbyname("");

To get the local IP, you should be using gethostname() instead of
gethostbyname("").

>     memcpy(&mjinad,mhstj->h_addr_list[ii++],sizeof(in_addr));
>     cret+="\n"+AnsiString(inet_ntoa(mjinad));

You don't need to copy the address before converting it.  You can convert
the original value directly:

    cret += "\n" + AnsiString(inet_ntoa(* (in_addr*)
mhstj->h_addr_list[ii++]);

> In BCB5 I get the IP details as expected. In BDS2006 error
> 10093 is returned implying something about WSAStartup not
> being called.

As well it should be.  You have to call WSAStartup() before calling any
other socket-related functions.  You should be doing that in any version of
BCB.  If BCB 5 is not requiring you to do that, then you likely have a
package installed that is calling WSAStartup() automatically during its
initialization.  You should not be relying on that behavior in your own
code.  Always explitically call WSAStartup() and WSACleanup() in any socket
code that you write yourself.

> Having looked at the structure required to set up a call to WSAStartup, I
am not happy.

You don't need to set up anything in order to call WSAStartup().  It only
has 1 input parameter, the requested version.  The WSADATA structure is an
output parameter so that you know which version is actually being used,
which may be different than the version you request.  You can ignore the
structure if you are only going to be using socket operations that are
supposed all the way back to v1.0, ie:

    bool WasInitialized = false;

    __fastcall TForm1::TForm1(TComponent *Owner)
        : TForm()
    {
        // can't use WSAGetLastError() with WSAStartup()...
        WSADATA wsa = {0};
        int ret = WSAStartup(MAKEWORD(1, 0), &wsa);
        WasInitialized = ( ret != 0 );
        if( !WasInitialized )
            MessageBox(NULL, ("Startup Error " + AnsiString(ret)).c_str(),
"Error", MB_OK | MB_ICONERROR);
    }

    __fastcall TForm1::~TForm1()
    {
        if( WasInitialized )
            WSACleanup();
    }

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        //...
    }

> Why doesn't this work as is?

Because it is not supposed to in the first place.


Gambit
Technical Details
Message-ID: <4485b07c$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <44855a92$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,ef067702696d334e
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: <44855a92$1@newsgroups.borland.com>
Subject: Re: BDS2006 requires WSAStartup be called...
Date: Tue, 6 Jun 2006 09:42:32 -0700
Lines: 73
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: <4485b07c$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1149612156 68.69.17.41 (6 Jun 2006 09:42:36 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail