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





Article #17113: ip address, machine name, TCP, internet, winsock

 Question and Answer Database
FAQ2113C.txt ip address, machine name, TCP, internet, winsock
Category :Windows API
Platform :All
Product :C++Builder 1.x
Question:
How do I get the local internet machine name and ip address?
Answer:
Getting the local machine name and ip address is a straight
forward process that is most easily accomplished using the TCP
component. Simply drop a TCP component from the internet page
of the component palette on to a form, and access the following
members of the TCP component:
Memo1->Lines->Add(TCP1->LocalHostName);
Memo1->Lines->Add(TCP1->LocalIp);
If you prefer not to use the TCP component, here is an example
that interfaces directly with your winsock layer:
#include 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
hostent *P;
char s[128];
in_addr in;
char *P2;
gethostname(s, 128);
P = gethostbyname(s);
Memo1->Lines->Add(P->h_name);
in.S_un.S_un_b.s_b1 = P->h_addr_list[0][0];
in.S_un.S_un_b.s_b2 = P->h_addr_list[0][1];
in.S_un.S_un_b.s_b3 = P->h_addr_list[0][2];
in.S_un.S_un_b.s_b4 = P->h_addr_list[0][3];
P2 = inet_ntoa(in);
Memo1->Lines->Add(P2);
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
WORD wVersionRequested;
WSADATA WSAData;
wVersionRequested = MAKEWORD(1,1);
WSAStartup(wVersionRequested,&WSAData);
}
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
WSACleanup();
}
7/2/98 10:32:32 AM

Last Modified: 01-SEP-99