各位高手,请问用API怎样获得本机的网卡的IP地址

xiaoguoru 2002-11-18 04:54:26
同上.
...全文
59 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingbin 2002-11-19
  • 打赏
  • 举报
回复
不是吧,我可以取得到IP啊,你再试试看。
xiaoguoru 2002-11-19
  • 打赏
  • 举报
回复
楼主的:gethostname(s, 128); p = gethostbyname(s);中的 s 与p 都返回为空.
xiaoguoru 2002-11-19
  • 打赏
  • 举报
回复
谢谢各位:我现在就给分.
warton 2002-11-19
  • 打赏
  • 举报
回复
例子多的很,找一个看吧!!
ljianq 2002-11-19
  • 打赏
  • 举报
回复
AnsiString __fastcall TComputerIPAddress::GetIPAddress()
{
struct hostent *thisHost;
struct in_addr in;
char MyName[80];
char *Ip,*ptr;

WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we couldn't find a usable */
/* WinSock DLL. */
return NULL;
}
/* Confirm that the WinSock DLL supports 2.0.*/
/* Note that if the DLL supports versions greater */
/* than 2.0 in addition to 2.0, it will still return */
/* 2.0 in wVersion since that is the version we */

/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
/* Tell the user that we couldn't find a usable */
/* WinSock DLL. */
WSACleanup( );
return NULL;
}

if(gethostname(MyName,80)==SOCKET_ERROR)
return NULL;

if(!(thisHost=gethostbyname(MyName)))
return NULL;
memset((void *)&in,sizeof(in),0);
in.s_addr=*((unsigned long *)thisHost->h_addr_list[0]);
if(!(ptr=inet_ntoa(in)))
return NULL;
WSACleanup( );
Ip=new char[strlen(ptr)+1];
strcpy(Ip,ptr);
return Ip;
}
szbug 2002-11-19
  • 打赏
  • 举报
回复
//Win32API:
// WSAStartup
// WSACleanup
// gethostname
// gethostbyname

#include <windows.h>
#include <iostream.h>

void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
MessageBox(NULL, "Wrong WinSock Version.", "Error", MB_OK);
return;
}

const int iChkIpTimes = 26;
int localIPnums = 0;
char hostName[80];
LPHOSTENT lpHostEnt;
struct in_addr addr[iChkIpTimes];

for ( int i=0; i<iChkIpTimes; i++)
{
memset(&addr[i], 0, sizeof(in_addr));
}

if ( gethostname(hostName, sizeof(hostName)) == SOCKET_ERROR )
{
MessageBox(NULL, "Can't getting local host name.", "Error", MB_OK);
WSACleanup();
return;
}
cout << "Local host name:" << hostName << endl;
lpHostEnt = gethostbyname( hostName );
if ( !lpHostEnt )
{
MessageBox(NULL, "Bad host lookup!", "Error", MB_OK);
WSACleanup();
return;
}
for ( int i=0; i<iChkIpTimes; i++)
{
if ( lpHostEnt->h_addr_list[i] == 0 )
{
localIPnums = i;
break;
}
memcpy(&addr[i], lpHostEnt->h_addr_list[i], sizeof(in_addr));
}

for ( int i=0; i<localIPnums; i++)
{
cout << "Local IP Address " << i << " : " << inet_ntoa(addr[i]) << endl;
}

if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup();
return;
}
}
lingbin 2002-11-18
  • 打赏
  • 举报
回复
给你点代码:
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);

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧