本文整理汇总了C++中HostInfo::addAddr方法的典型用法代码示例。如果您正苦于以下问题:C++ HostInfo::addAddr方法的具体用法?C++ HostInfo::addAddr怎么用?C++ HostInfo::addAddr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HostInfo
的用法示例。
在下文中一共展示了HostInfo::addAddr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getHostInfo
//host information mode
int getHostInfo(int argc, const char* arg, HostInfo& h)
{
hostent* remoteHost;
unsigned int addr;
#ifdef WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
char* hostname = (char*) malloc(40);
strcpy(hostname, arg);
if(isalpha(hostname[0]) != 0)
{
//name -> ipaddr
hostname[strlen(hostname)] = '\0';
remoteHost = gethostbyname(hostname);
if(!remoteHost)
{
cout << "Cannot resolve host information: " << WSAGetLastError() << endl;
}
else
{
h.setName(string(remoteHost->h_name));
struct in_addr rawaddr;
if(remoteHost -> h_addrtype == AF_INET)
{
for(int i=0; remoteHost->h_addr_list[i] != 0; i++)
{
rawaddr.s_addr = *(u_long*) remoteHost->h_addr_list[i];
h.addAddr(string(inet_ntoa(rawaddr)));
}
}
}
}
else
{
//num
addr = inet_addr(hostname);
remoteHost = gethostbyaddr((char*) &addr, 4, AF_INET);
if(!remoteHost)
{
cout << "Cannot resolve host information: " << WSAGetLastError() << endl;
}
else
{
cout << remoteHost->h_name << endl;
h.setName(string(remoteHost->h_name));
h.addAddr(hostname);
}
}
WSACleanup();
#else
cout << "Windows only function" << endl;
#endif
return 0;
}