源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C语言获得电脑的IP地址的小例子

  • 时间:2022-07-14 01:23 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C语言获得电脑的IP地址的小例子
[u]复制代码[/u] 代码如下:
#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%s\n[Name]\t%s\n\n", pszAddr, host_name);          }      }      else      {          printf("ERROR\n");      }      /* WSACleanup() finish use Winsock 2 DLL (Ws2_32.dll). Head:Winsock2.h. reference #pragma comment(lib, "ws2_32.lib") */      WSACleanup();      return 0;  }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部