获取本机主机名称和IP地址
平臺:VS2017
?
#include "pch.h"
 #define _CRT_SECURE_NO_WARNINGS?? ??? ?
 #include <winsock2.h>
 #include <ws2tcpip.h>
 #include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")
int ?main(int argc, char **argv)
 {
 ?? ?struct hostent* phost;?? ?
 ?? ?char ip[20];?? ?
 ?? ?char hostname[100];?
 ?? ?WORD wVersion;
 ?? ?WSADATA wsadata;?? ?
 ?? ?if (0 != WSAStartup(MAKEWORD(2, 2), &wsadata)) ? //初始化?? ?
 ?? ?{?? ??? ??? ??? ?
 ?? ??? ?return NULL;?? ?
 ?? ?}?? ?
 ?? ?gethostname(hostname, 100);?? ?
 ?? ?phost = gethostbyname(hostname); ?? ?
 ?? ?char *pIP = inet_ntoa(*(in_addr *)phost->h_addr_list[0]);//將32位IP轉化為字符串IP?? ?
 ?? ?WSACleanup();//釋放Winsock API
 ?? ?printf("%s\n", pIP);
 ?? ?return 0;
 }
 ?
總結
以上是生活随笔為你收集整理的获取本机主机名称和IP地址的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: getaddrinfo函数
- 下一篇: vprintf() 函数的用法。
