Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43833
  • 博文数量: 21
  • 博客积分: 1425
  • 博客等级: 上尉
  • 技术积分: 215
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-29 21:56
文章分类

全部博文(21)

文章存档

2009年(19)

2008年(2)

我的朋友

分类: C/C++

2009-09-05 11:28:25

//仅仅想要获得计算机名字:  
  char   name[100];  
  DWORD   i=100;         //   注意只能是DWORD类型.不能是int  
  GetComputerName(name, &i);  
  MessageBox(name);

//想要获得ip地址:  
  WORD   wVersionRequested;  
  WSADATA   wsaData;  
  int   err;  
  wVersionRequested   =   MAKEWORD(   2,   2   );  
  err   =   WSAStartup(   wVersionRequested,   &wsaData   );  
//根据err的值判断是否成功.  
char HostName[100];  
gethostname(HostName, 100);//   获得本机主机名.  
hostent* hn;  
hn = gethostbyname(HostName);//根据本机主机名得到本机ip  
CString strIPAddr;  
strIPAddr = inet_ntoa(*(struct in_addr *)hn->h_addr_list[0]);//把ip换成字符串形  
MessageBox(strIPAddr);
strIPAddr = inet_ntoa(*(struct in_addr *)hn->h_addr_list[1]);//网卡2(虚拟网卡)  
MessageBox(strIPAddr);
strIPAddr = inet_ntoa(*(struct in_addr *)hn->h_addr_list[2]);//网卡3(虚拟网卡)
MessageBox(strIPAddr);

//strIPAddr = inet_ntoa(*(struct in_addr *)hn->h_addr_list[3]);//无此网卡(程序出错)
//MessageBox(strIPAddr);
 
bool GetLocHostIp(char *szIp)
{
 char szHostName[128];  
 if(gethostname(szHostName, 128) == 0) //计算机名, 变量szHostName就是本机名 
 {   
  struct hostent *pHost;
  int i;
  pHost = gethostbyname(szHostName);
  for(i = 0; pHost != NULL && pHost->h_addr_list[i]!=NULL; i++)
  {
   char strHostIp[25];
   memset(strHostIp, 0, 25);
   int j;
   for(j = 0; j < pHost->h_length; j++)
   {
    char addr[10];
    if(j > 0)
     strcat(strHostIp, ".");
    sprintf(addr, "%u",(unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
    TRACE("addr = %s\t",  addr);
    strcat(strHostIp, addr);
   }
   strcpy(szIp, strHostIp);
   TRACE("szIP = %s\n", szIp);
  }  
 }
 return true;
}
阅读(576) | 评论(0) | 转发(0) |
0

上一篇:GetFileSize

下一篇:port_reuse

给主人留下些什么吧!~~