各位大侠:
    小弟刚开始学winsock编程,一个聊天程序中想要获取聊天对方的主机名(或者说整个hostent),如何办到?

解决方案 »

  1.   

    gethostname
    The gethostname function retrieves the standard host name for the local computer.int gethostname(
      char* name,
      int namelen
    );Parameters
    name 
    [out] Pointer to a buffer that receives the local host name. 
    namelen 
    [in] Length of the buffer, in bytes. 
    Return Values
    If no error occurs, gethostname returns zero. Otherwise, it returns SOCKET_ERROR and a specific error code can be retrieved by calling WSAGetLastError.
      

  2.   

    可以使用 getpeername 函数根据 socket 句柄获得对端的主机等信息。
    getpeername 函数的使用方法可以查看MSDN。
      

  3.   

    如何获取局域网上计算机名及它们的IP地址
    l    连接ws2_32.lib和 mpr.lib库
    l    #include winsock2.h
    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];
        WNetEnumResource( hEnum, &Count, 
            Buffer, &BufferSize );
        NetResource = (NETRESOURCE*)Buffer;    char szHostName[200];
        unsigned int i;    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);                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);
                    AfxMessageBox(strTemp);
                }
            }
        }    delete Buffer;
        WNetCloseEnum( hEnum ); 
    }WSACleanup();
      

  4.   

    楼上的太复杂了吧,有没有使用MFC CAsyncSocket类成员函数或者使用WINSOCK的API的方法?
      

  5.   

    当我的回复不存在?
    hostent *pHost;
    in_addr addr;
    addr.S_un.S_addr = inet_addr("192.168.0.1");
    pHost = gethostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
    AfxMessageBox(pHost->h_name);
      

  6.   

    getremoteaddr()
    {
    struct sockaddr_in sockaddr;
    int nlen = sizeof(sockaddr); memset(&sockaddr, 0, sizeof(sockaddr)); getpeername(m_socket, (struct sockaddr*)&sockaddr, &nlen); m_straddr = inet_ntoa(sockaddr.sin_addr); return m_straddr.data();
    }
      

  7.   

    Hendy_So(),你回答我看见了呵呵,不好意思。不过你的程序里"192.168.0.1"已经指定了ip了呀。谢谢aiyue2010(亚伦) 的回答。
      

  8.   

    Hendy_So(),你的gethostbyaddr()函数好像使用也有问题,我试过了
      

  9.   

    楼主还没搞定吗?转贴给你一个函数介绍
       getpeername()函数
                                    这个函数太简单了。
                                  它太简单了,以至我都不想单列一章。但是我还是这样做了。 函数 getpeername() 
                                  告诉你在连接的流式套接字上谁在另外一边。函 数是这样的:
                                  #include <sys/socket.h>
                                  int getpeername(int sockfd, struct sockaddr *addr, 
                                  int *addrlen);
                                  sockfd 是连接的流式套接字的描述符。addr 是一个指向结构 struct sockaddr 
                                  (或者是 struct sockaddr_in) 的指针,它保存着连接的另一边的 
                                  信息。addrlen 是一个 int 型的指针,它初始化为 sizeof(struct 
                                  sockaddr)。 函数在错误的时候返回 -1,设置相应的 errno。
                                  一旦你获得它们的地址,你可以使用 inet_ntoa() 或者 gethostbyaddr() 
                                  来打印或者获得更多的信息。但是你不能得到它的帐号。(如果它运行着愚 
                                  蠢的守护进程,这是可能的,但是它的讨论已经超出了本文的范围,请参 考 RFC-1413 
                                  以获得更多的信息。)