Главная страница | назад





Article #19707: Getting local hostname from winsock.

 Question and Answer Database

     FAQ: FAQ4707C - Getting local hostname from winsock.
Category: Internet/WEB
Platform: All-32Bit
 Product: C++Builder3.x,   C++Builder4.x,   

Question:

How do I get the local internet machine name and IP address?


Answer:

Here is an example that uses the Winsock API to get the local hostname.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    hostent *p;
    char s[128];
    char *p2;
    //Get the computer name
    gethostname(s, 128);
    p = gethostbyname(s);
    Memo1->Lines->Add(p->h_name);
    //Get the IpAddress
    p2 = inet_ntoa(*((in_addr *)p->h_addr));
    Memo1->Lines->Add(p2);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    WORD wVersionRequested;
    WSADATA wsaData;
    //Start up WinSock
    wVersionRequested = MAKEWORD(1, 1);
    WSAStartup(wVersionRequested, &wsaData);
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    WSACleanup();
}



8/4/99 11:56:35 AM
 

Last Modified: 01-SEP-99