windows 下得到主机IP地址可以用gethostbyname(),gethostbyaddr()
#include <afxsock.h>
if( !AfxSockedInit())
return;
struct hostent *ph;
int GetIP(char* strHost,char* strIP,
CString strHost = "163.com";
struct inet_addr addr;
if( inet_addr(strHost) == INADDR_NONE)
{
ph = gethostbyname(strHost);
}
else
{
addr = inet_addr(strHost);
ph = gethostbyAddr((char*)&addr,sizeof(addr),AF_INET);
}
if( ph == NULL)
return false;
hostent 的结构为:
struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};
#define h_addr h_addr_list[0]
这里是这个数据结构的详细资料:
struct hostent:
h_name – 地址的正式名称。
h_aliases – 空字节-地址的预备名称的指针。
h_addrtype –地址类型; 通常是AF_INET。
h_length – 地址的比特长度。
h_addr_list – 零字节-主机网络地址指针。网络字节顺序。
h_addr - h_addr_list中的第一地址。
ACE 下:
#include "ace/ace.h"
#include "ace/INET_Addr.h"
ACE::Init() //初始化,否则不能得到正确的地址
ACE_INET_Addr addr_;
if( addr.set(port,servername)!= 0)
return false;
#include <afxsock.h>
if( !AfxSockedInit())
return;
struct hostent *ph;
int GetIP(char* strHost,char* strIP,
CString strHost = "163.com";
struct inet_addr addr;
if( inet_addr(strHost) == INADDR_NONE)
{
ph = gethostbyname(strHost);
}
else
{
addr = inet_addr(strHost);
ph = gethostbyAddr((char*)&addr,sizeof(addr),AF_INET);
}
if( ph == NULL)
return false;
hostent 的结构为:
struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};
#define h_addr h_addr_list[0]
这里是这个数据结构的详细资料:
struct hostent:
h_name – 地址的正式名称。
h_aliases – 空字节-地址的预备名称的指针。
h_addrtype –地址类型; 通常是AF_INET。
h_length – 地址的比特长度。
h_addr_list – 零字节-主机网络地址指针。网络字节顺序。
h_addr - h_addr_list中的第一地址。
ACE 下:
#include "ace/ace.h"
#include "ace/INET_Addr.h"
ACE::Init() //初始化,否则不能得到正确的地址
ACE_INET_Addr addr_;
if( addr.set(port,servername)!= 0)
return false;