//取得文件接收方 IP地址
ent=::gethostbyname(hostname);
if(ent==NULL)
{
return;
}
//ent 中IP地址就是网络字节序
host.addr=*(ULONG *)ent->h_addr_list[0];
通过自己的主机名获取自己的IP成功
通过局域网其他机子主机机获取其IP返回ent==NULL?
//////////套接字初始化用
if(!AfxSocketInit())
{
AfxMessageBox("加载套接字失败!");
return false;
}
ent=::gethostbyname(hostname);
if(ent==NULL)
{
return;
}
//ent 中IP地址就是网络字节序
host.addr=*(ULONG *)ent->h_addr_list[0];
通过自己的主机名获取自己的IP成功
通过局域网其他机子主机机获取其IP返回ent==NULL?
//////////套接字初始化用
if(!AfxSocketInit())
{
AfxMessageBox("加载套接字失败!");
return false;
}
//先清空列表
list.RemoveAll(); CString strTemp;
struct hostent *host;
struct in_addr *ptr; // 获得IP地址 DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum ); WSADATA wsaData;
//开始枚举网络资源
WSAStartup(MAKEWORD(1,1),&wsaData); if ( hEnum ) //如果句柄有效
{
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 2048;
LPVOID Buffer = new char[2048];
// 调用WSAStartup后调用WNetEnumResource做进一步的枚举工作
WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );
NetResource = (NETRESOURCE*)Buffer; char szHostName[200]; for ( unsigned int i = 0; i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++ )
{
if ( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY ) {
if ( NetResource->lpRemoteName )
{
CString strFullName = NetResource->lpRemoteName;
if ( 0 == strFullName.Left(2).Compare("\\\\") ) strFullName = strFullName.Right(strFullName.GetLength()-2);
//获得主机名
gethostname( szHostName, strlen( szHostName ) );
//由主机名获得跟它对应的主机信息
host = gethostbyname(strFullName);
if(host == NULL) continue;
ptr = (struct in_addr *) host->h_addr_list[0];
// 提取IP地址信息,地址形式如下: 211.40.35.76
int a = ptr->S_un.S_un_b.s_b1; // 211
int b = ptr->S_un.S_un_b.s_b2; // 40
int c = ptr->S_un.S_un_b.s_b3; // 35
int d = ptr->S_un.S_un_b.s_b4; // 76 strTemp.Format("%s --> %d.%d.%d.%d",strFullName,a,b,c,d);
// 加入到链表中
list.Add(strTemp);
}
}
}
delete Buffer;
// 结束枚举工作
WNetCloseEnum( hEnum );
} // 卸载Winsock.dll
WSACleanup();