C語言獲得電腦的IP地址的小例子
更新時間:2013年05月15日 09:40:18 作者:
C語言獲得電腦的IP地址的小例子,需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
#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;
}
您可能感興趣的文章:
- 怎么通過C語言自動生成MAC地址
- c語言中單引號和雙引號的區(qū)別(順利解決從字符串中提取IP地址的困惑)
- C++獲取當(dāng)前進(jìn)程IAT的方法
- C++實現(xiàn)獲取IP、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS等本機(jī)網(wǎng)絡(luò)參數(shù)的方法
- c++實現(xiàn)發(fā)送http請求通過get方式獲取網(wǎng)頁源代碼
- C++訪問注冊表獲取已安裝軟件信息列表示例代碼
- C++獲取本機(jī)登陸過的QQ號碼示例程序
- C++中獲取UTC時間精確到微秒的實現(xiàn)代碼
- C++獲取zip文件列表方法
- C++獲取本機(jī)MAC,IP,MASK地址的方法
相關(guān)文章
C++ new與malloc和delete及free動態(tài)內(nèi)存管理及區(qū)別介紹
這篇文章主要介紹了深入理解C++中的new/delete和malloc/free動態(tài)內(nèi)存管理,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12C/C++哈希表優(yōu)化LeetCode題解997找到小鎮(zhèn)的法官
這篇文章主要為大家介紹了C/C++哈希表優(yōu)化題解997找到小鎮(zhèn)的法官示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12