Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4460979
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: C/C++

2008-05-06 20:17:43


#include
#include "winsock2.h "
#include
#include
using namespace std;
int main()  
{  
 struct   hostent   *thisHost;  
    struct   in_addr   in;  
    char   MyName[80];  
    char   *Ip,*ptr;  
   
    WORD   wVersionRequested;  
    WSADATA   wsaData;  
    int   err;  
    wVersionRequested   =   MAKEWORD(   2,   0   );  
    err   =   WSAStartup(   wVersionRequested,   &wsaData   );  
    if   (   err   !=   0   )  
 {  
  /*   Tell   the   user   that   we   couldn't   find   a   usable   */  
  /*   WinSock   DLL.                                                                     */  
  return   -1;  
 }  
    /*   Confirm   that   the   WinSock   DLL   supports   2.0.*/  
 /*   Note   that   if   the   DLL   supports   versions   greater         */  
 /*   than   2.0   in   addition   to   2.0,   it   will   still   return   */  
 /*   2.0   in   wVersion   since   that   is   the   version   we             */  
   
 /*   requested.                                                                                 */  
   
 if   (   LOBYTE(   wsaData.wVersion   )   !=   2   ||  
  HIBYTE(   wsaData.wVersion   )   !=   0   )  
 {  
  /*   Tell   the   user   that   we   couldn't   find   a   usable   */  
  /*   WinSock   DLL.                                                                     */  
  WSACleanup();  
  return   -1;  
 }  
   
    if(gethostname(MyName,80)==SOCKET_ERROR)  
        return  -1;  
   
    if(!(thisHost=gethostbyname(MyName)))  
        return   -1;  
    memset((void   *)&in,sizeof(in),0);  
    in.s_addr=*((unsigned   long   *)thisHost->h_addr_list[0]);  
    if(!(ptr=inet_ntoa(in)))  
        return   -1;  
    WSACleanup();  
    Ip=new char[strlen(ptr)+1];  
    strcpy(Ip,ptr); 
 cout << Ip;
 delete[] Ip;
 return 0;
 
}
阅读(1904) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~