我的渴望来自你的热情

解决方案 »

  1.   

    getpeername 获得SOCKET对等方的地址,从中取出IP地址
    然后用gethostbyaddr获得该主机名
      

  2.   

    GetPeerName()可以解决你的问题
      

  3.   

    也可以用枚举的方法去找到lan里的所有的机器。void CL1Dlg::GetLanActiveComputer()
    {
    CString strTemp;
    struct hostent *host; DWORD dwScope=RESOURCE_CONTEXT;
    NETRESOURCE *NetResource=NULL;
    HANDLE hEnum; WNetOpenEnumA(
    dwScope,
    NULL,
    NULL,
    NULL,
    &hEnum); if (hEnum)
    {
    DWORD Count=0xFFFFFFFF;
    DWORD BufferSize=2048;
    LPVOID Buffer=new char[2048];
    WNetEnumResourceA(
    hEnum,
    &Count,
    Buffer,
    &BufferSize);

    NetResource = (NETRESOURCE*)Buffer; unsigned int i;
    //---------------------------------------------
    //从其中得到在lan里的可以连接的机器。
    for(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);
    host=gethostbyname(strFullName);
    if (host==NULL)
    {
    continue;
    }
    strTemp.Format("%s",strFullName);
    m_mlist.InsertItem(i,strTemp,0); //从计算机名得到对应的IP地址。
    struct hostent *lpHostEnt= gethostbyname(strTemp);
    LPSTR lpAddr=lpHostEnt->h_addr_list[0];
    if (lpAddr)
    {
    struct in_addr inAddr;
    memmove(&inAddr,lpAddr,4);
    sIpAddr=inet_ntoa(inAddr);
    } //写入一个ini文件中。
    ::WritePrivateProfileString(
    "IPTOADD",
    strTemp,
    sIpAddr,
    ".\\ip.ini");
    }
    }
    }
    }
    delete Buffer;
    WNetCloseEnum(hEnum);
    }}
      

  4.   

    只需通过两个函数 GETHOSTNAME 和 GETHOSTBYNAME 变可解决问题
      

  5.   

    Thank All of you !