取本机多个IP地址问题

yunicorn 2003-02-20 06:05:08
我的机器有多个IP地址,请问在BCB中用何种方法可以都显示出来?能否用例子详述,谢谢
...全文
54 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
warton 2003-02-24
  • 打赏
  • 举报
回复
老贴子中很多
jishiping 2003-02-23
  • 打赏
  • 举报
回复
点帖子右上角的管理,就可以给分了。
yunicorn 2003-02-23
  • 打赏
  • 举报
回复
谢谢各位,不知道怎么给你门分数,或是已经给了?
HUANG_JH 2003-02-20
  • 打赏
  • 举报
回复
老侃的代码,借花献佛而已
HUANG_JH 2003-02-20
  • 打赏
  • 举报
回复
#include <windows.h>
#include <winsock.h>
#include <conio.h>
#include <stdio.h>


//Main function
int main(int argc, char **argv)
{
WORD wVersionRequested;
WSADATA wsaData;
LPSTR szIPAddr;
//Start up WinSock
wVersionRequested = MAKEWORD(1, 1);
if(WSAStartup(wVersionRequested, &wsaData))
{
printf("ERROR No.1!!! Program Terminate.");
return 0;
}

char s[128];
char *p2;
DWORD dwIPAddr;
//Get the computer name
if(gethostname(s, 128)==SOCKET_ERROR)
{
printf("ERROR No.2!!! Program Terminate.");
return 0;
}

p = gethostbyname(s);
if(!p)
{
printf("ERROR!!! Bad host lookup. Program Terminate.");
return 0;
}

printf("Local Computer Name: %s\n",s);
printf("Local Host Name: %s\n",p->h_name);
int i=0;
while(p->h_aliases[i])
{
printf(" %s\n",p->h_aliases[i]);
i++;
}


//Get the IpAddress,这里可以对付多个IP地址的主机
for(int i=0;p->h_addr_list[i]!=0;i++)
{
p2 = inet_ntoa(*((in_addr *)p->h_addr_list[i]));
printf("Local IP Adress: %s\n",p2);
}
}
jishiping 2003-02-20
  • 打赏
  • 举报
回复
不知道下面的代码是否可以。

char *IPAddr;
char name[65];
struct hostent *ph;
struct WSAData Data;

if (!WSAStartup(0x101,&Data)) {
memset(name, 0, sizeof(name));
gethostname(name, sizeof(name));
ph = gethostbyname(name);
if (ph != NULL) {
in_addr* addr = (in_addr*)ph->h_addr;
IPAddr = inet_ntoa(addr[0]); //返回 IP 地址
if (IPAddr != NULL)
MessageBox(0, IPAddr, "IP1", MB_OK);
IPAddr = inet_ntoa(addr[1]); //第2块网卡的IP地址
if (IPAddr != NULL)
MessageBox(0, IPAddr, "IP2", MB_OK);
}
WSACleanup();
}

1,317

社区成员

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

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