C++控制台程序获取本机IP地址

来源:互联网 发布:java后端接口开发技术 编辑:程序博客网 时间:2024/06/03 15:26
#include <windows.h>


#include <winsock.h>


#include <string.h>


#include <iostream.h>


//using namespace std;


#pragma comment(lib,"wsock32.lib")


int main()


{

char buf[256]="";

struct hostent *ph = 0;

WSADATA w;

WSAStartup(0x0101, &w);

gethostname(buf, 256);

//String hostNmae = buf;//此处获得本机名称

ph = gethostbyname(buf);

const char *IP =inet_ntoa(*((struct in_addr *)ph->h_addr_list[0]));//此处获得本机IP


cout<<IP<<endl;//显示本机IP

WSACleanup();

return 0;

}
原创粉丝点击