sinat_34458693 2016-06-13 09:32 采纳率: 0%
浏览 2706

C++socket获取ip地址问题

#include "stdafx.h"
#include
#include
#include
#include
#include
#pragma comment(lib, "ws2_32.lib ") //linking to the library
using namespace std;

int main(int argc, char **argv) {

WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);//initiate the ws2_32.dll and match the version
if (err != 0)
{
    return 0;
}
if (LOBYTE(wsaData.wVersion) != 1 ||   //if the version is not matched ,then quit and terminate the ws3_32.dll 
    HIBYTE(wsaData.wVersion) != 1)
{
    WSACleanup();
    return 0;
}
struct addrinfo *ailist, *aip;
struct addrinfo hint;
struct sockaddr_in6 *sinp6;
PHOSTENT hostinfo;
char *hostname;    //主机名   
char *port = "3294";                              //端口号 
const char *addr;
int ilRc;


if (argv[1] = NULL) {
    gethostname(hostname, sizeof(hostname));
}
else {
    hostname = argv[1];
}
if ((hostinfo = gethostbyname(hostname)) == NULL)    //获得本地ipv4地址
{
    errno = GetLastError();
    fprintf(stderr, "gethostbyname Error:%d\n", errno);
    return 1;
}
LPCSTR ip;
while (*(hostinfo->h_addr_list) != NULL)        //输出ipv4地址
{
    ip = inet_ntoa(*(struct in_addr *) *hostinfo->h_addr_list);
    printf("ipv4 addr = %s\n\n", ip);
    hostinfo->h_addr_list++;
}

hint.ai_family = AF_INET6;                 
hint.ai_socktype = SOCK_STREAM;     
hint.ai_flags = AI_PASSIVE;                   
hint.ai_protocol = 0;                              
hint.ai_addrlen = 0;                                
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
ilRc = getaddrinfo(hostname, port, &hint, &ailist);   
if (ilRc < 0)
{
    char str_error[100];
    strcpy(str_error, (char *)gai_strerror(errno));
    printf("str_error = %s", str_error);
    return 0;
}
if (ailist == NULL)
{
    printf("sorry not find the IP address,please try again \n");
}
for (aip = ailist; aip != NULL; aip = aip->ai_next)                         
{
    aip->ai_family = AF_INET6;
    sinp6 = (struct sockaddr_in6 *)aip->ai_addr;                                  
    int i;
    printf("ipv6 addr = ");
    for (i = 0; i < 16; i++)
    {
        if (((i - 1) % 2) && (i>0))
        {
            printf(":");
        }
        printf("%02x", sinp6->sin6_addr.u.Byte[i]);
    }
    printf(" \n");
    printf(" \n");
}


system("pause");
return 0;

}

代码如上,想实现的功能是从cmd进入后不输入参数输出本地ipv4 ipv6地址,输入域名可以输出指定域名的ipv4 ipv6地址

但是调试结果ipv6显示出错,全0,指定域名后也只会输出主机的地址,求各位大神看看,谢了

  • 写回答

2条回答 默认 最新

  • Robot-C 2016-06-13 09:41
    关注


    写远控时用到的:
     
     
        SOCKADDR_IN addrIp;
        int addrIpsize=sizeof(addrIp);
        getpeername(clinet,(SOCKADDR*)&addrIp,&addrIpsize);
     
        char ipadd[20];
        strcpy_s(ipadd,inet_ntoa(addrIp.sin_addr));
     ......
    答案就在这里:通过socket获取ip地址
    ----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。