小女求助,如何得到自己电脑在网络中的IP地址及端口号,谢谢帮忙

解决方案 »

  1.   

    IP简单,"不过本机端口"--指的什么? 是port吗?port只是应用层概念,本机可以有0~~25565(好像是吧)号端口不用来表示计算机.
    这样说你明白吗.
    *****************************get local ip source code****************
    char szhost[256] = {0};
    HOSTENT *host = 0;
    in_addr HostIP = {0};
    SOCKADDR_IN ip = {0};
    if(!gethostname(szhost,256))
    AfxMessageBox(szhost);
    host = gethostbyname( szhost );
    if(host)
    {
    AfxMessageBox(host->h_name);
    memcpy(&HostIP, host->h_addr_list[0], sizeof(in_addr));
    ip.sin_addr.S_un.S_addr = ntohl(HostIP.S_un.S_addr);
    AfxMessageBox( inet_ntoa(HostIP) );
    }
    *************************** end **************************************
      

  2.   

    CString GetIp()
    {
          WORD wVersionRequested;
          WSADATA wsaData;
          char name[255];
          CString ip;
          PHOSTENT hostinfo;
          wVersionRequested = MAKEWORD( 2, 0 );      if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
          {            if( gethostname ( name, sizeof(name)) == 0)
                {
                      if((hostinfo = gethostbyname(name)) != NULL)
                      {
                            ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
                      }
                }
                
                WSACleanup( );
          } 
      return ip;
    }端口不知, 用端口扫描软件扫吧
    或者笨办法, bind