随笔 - 0  文章 - 5  trackbacks - 0
<2025年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

常用链接

留言簿(2)

文章分类

文章档案

教育

信息学奥赛

有用网站

在线OJ

专题测试

租房信息

搜索

  •  

最新评论

#include <iostream>
#include 
<fstream>
using namespace std;
#include 
"winsock2.h"
#pragma comment(lib,
"ws2_32.lib")
ofstream fout(
"info.cpp");
int main()
{
//初始化:如果不初始化,以下代码将无法执行
WSAData data;
if(WSAStartup(MAKEWORD(1,1),&data)!=0)
{
   cout
<<"初始化错误,无法获取主机信息"<<endl ;
}
 
char host[255];
//获取主机名:也可以使用GetComputerName()这个函数
if(gethostname(host,sizeof(host))==SOCKET_ERROR)
{
   cout
<<"无法获取主机名"<<endl;
}
else
{
   cout
<<"本机计算机名为: "<<host<<endl;
   fout
<<"本机计算机名为: "<<host<<endl;
}

//获取计算机IP:gethostbyname也需要初始化(上面已初始化)
struct hostent *p=gethostbyname(host);
if(p==0)
{
   cout
<<"无法获取计算机主机名及IP"<<endl;
}
else
{
   
//获取本机计算机名
   
//cout<<"本机计算机名为:"<<p->h_name<<endl;

   
//本机IP:利用循环,输出本机所有IP
   for(int i=0;p->h_addr_list[i]!=0;i++)
   {
    
struct in_addr in;
    memcpy(
&in,p->h_addr_list[i],sizeof(struct in_addr));
    cout
<<""<<i+1<<"块网卡的IP为: "<<inet_ntoa(in)<<endl;
    fout
<<""<<i+1<<"块网卡的IP为: "<<inet_ntoa(in)<<endl;
   }

}
WSACleanup();
cin.
get();
return 0;
}
posted on 2014-04-16 15:29 龙在江湖 阅读(473) 评论(0)  编辑 收藏 引用 所属分类: C++