C++ 获得本机IP

来源:互联网 发布:java读取dat文件内容 编辑:程序博客网 时间:2024/05/14 23:51



#include "stdafx.h"#include <winsock2.h>#include <stdio.h>#include <windows.h>#include <iostream> #pragma comment(lib,"ws2_32.lib")





char buf[256]=""; struct hostent *ph = 0; WSADATA w; WSAStartup(0x0101, &w);//这一行必须在使用任何SOCKET函数前写! gethostname(buf, 256); std::string hostNmae = buf;//此处获得本机名称 ph = gethostbyname(buf); const char *IP =inet_ntoa(*((struct in_addr *)ph->h_addr_list[0]));//此处获得本机IP WSACleanup(); printf("%s",IP);

原创粉丝点击