手机
当前位置:查字典教程网 >编程开发 >C语言 >C语言获得电脑的IP地址的小例子
C语言获得电脑的IP地址的小例子
摘要:复制代码代码如下:#include#include#pragmacomment(lib,"WS2_32.lib")intmain(){cha...

复制代码 代码如下:

#include <stdio.h>

#include <winsock2.h>

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

int main()

{

char host_name[256]; // define host name (for example:xxx-PC)

int WSA_return, i;

WSADATA WSAData;

HOSTENT *host_entry; // record host information

WORD wVersionRequested;

wVersionRequested = MAKEWORD(2, 0);

WSA_return = WSAStartup(wVersionRequested, &WSAData); // initialize Winsock service and then call other socket or dll file

if (WSA_return == 0) // initialize success

{

gethostname(host_name, sizeof(host_name));

host_entry = gethostbyname(host_name);

for(i = 0; host_entry != NULL && host_entry->h_addr_list[i] != NULL; ++i)

{

// define pszAddr to record IP

// inet_ntoa: Convert an IP into an Internet standard dotted format string

const char *pszAddr = inet_ntoa (*(struct in_addr *)host_entry->h_addr_list[i]);

printf("[IP]t%sn[Name]t%snn", pszAddr, host_name);

}

}

else

{

printf("ERRORn");

}

/* WSACleanup() finish use Winsock 2 DLL (Ws2_32.dll). Head:Winsock2.h. reference #pragma comment(lib, "ws2_32.lib") */

WSACleanup();

return 0;

}

【C语言获得电脑的IP地址的小例子】相关文章:

用c 获取文件MD5值的实现方法

深入解析C语言中常数的数据类型

c语言读取obj文件转换数据的小例子

浅析C语言中的内存布局

对C语言中递归算法的深入解析

关于C语言函数strstr()的分析以及实现

基于c语言中调试工具的用法汇总(不包含gdb)

C语言程序设计50例(经典收藏)

c语言:金币阵列的问题

C语言宏定义使用分析

精品推荐
分类导航