套接字编程获取本机的ip地址

#include "WinSock.h"
#include <iostream>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
int main()
{
 char*ip;
 char name[32];
 WSADATA wsaData;
 WORD sv = MAKEWORD(2, 0);
 WSAStartup(sv, &wsaData);
 PHOSTENT hostname;
 gethostname (name, sizeof(name));
 hostname = gethostbyname(name);
 ip = inet_ntoa (*(struct in_addr *)*hostname->h_addr_list);
 cout<<"IP:"<<ip<<endl<<"Name:"<<name<<endl;
 return 0;
}

posted @ 2012-12-23 22:51  forevermemory  阅读(341)  评论(0编辑  收藏  举报