#include <string.h>
#include <malloc.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
using namespace std
const char* dns_parse(const char* host) {
struct addrinfo hints
struct addrinfo *result = NULL
struct addrinfo *rp = NULL
memset(&hints, 0, sizeof(struct addrinfo))
hints.ai_family = AF_UNSPEC /* Allow any */
hints.ai_socktype = 0 /* Any */
hints.ai_flags = 0/* No flags */
hints.ai_protocol = 0 /* Any protocol */
int error = getaddrinfo(host, NULL, &hints, &result)
if (error != 0) {
if (result) {
freeaddrinfo(result)
}
return ""
}
const char* ip = ""
const char* ipv6 = ""
const char* ipv4 = ""
char tmp[INET6_ADDRSTRLEN * 2] = {0}
for (rp = resultrp != NULLrp = rp->ai_next) {
if (rp->ai_family == AF_INET &&strlen(ipv4) == 0) {
const char *ret = inet_ntop(rp->ai_family,
&((struct sockaddr_in*)rp->ai_addr)->sin_addr,
tmp, (INET6_ADDRSTRLEN * 2))
if (ret != NULL) {
ipv4 = ret
}
} else if (rp->ai_family == AF_INET6 &&strlen(ipv6) == 0) {
const char *ret = inet_ntop(rp->ai_family,
&((struct sockaddr_in6*)rp->ai_addr)->sin6_addr,
tmp, (INET6_ADDRSTRLEN * 2))
if (ret != NULL) {
ipv6 = ret
}
}
}
if (result) {
freeaddrinfo(result) // Not needed anymore
}
if (strlen(ipv6) >0) {
ip = ipv6
} else if (strlen(ipv4) >0) {
ip = ipv4
} else {
return ""
}
return ip
}
void save_to_file(char* data, int len, const char* file_path) {
FILE *fp = fopen(file_path, "wb")
if (!fp) {
return
}
fwrite(data, sizeof(char), len, fp)
fclose(fp)
}
int main() {
const char* ip = dns_parse("www.baidu.com")
printf("%s\n", ip)
int len = strlen(ip)
char* data = (char*)malloc(len + 1)
memcpy(data, ip, len)
data[len] = '\0'
save_to_file(data, len + 1, "a.txt")
free(data)
return 0
}
和命令没有什么关系。主要是要安装webserver。然后解析域名到IP,再到主机上绑定这个域名就可以了
建议使用护卫神主机大师,一键安装 webserver,在线绑定域名。非常简单方便。
1、知道对方计算机名查看对方ip方法:开始->运行->cmd->ping 对方计算机名
或者 开始->运行->cmd->nbtstat -a 对方计算机名
2、知道对方ip查看对方的计算机名
方法:开始->运行->cmd->net view 对方ip
或者 开始->运行->cmd->nbtstat -a 对方ip
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)