使用gethostbyname()提取IP数据
Extracting IP Data using gethostbyname()
现在,例如,如果我想找到某个主机的IP。我将使用gethostbyname()命令并将数据放在结构体host中。现在我想获得h_addr_list信息。我所做的就是开始打印地址,直到得到一个空字符。现在我面临的问题是地址是*char格式,我想把它们改成IPv4格式。我已经尝试了一些方法,但总是遇到问题。
struct hostent
{
char *h_name; /* Official domain name of host */
char **h_aliases; /* Null-terminated array of domain names */
int h_addrtype; /* Host address type (AF_INET) */
int h_length; /* Length of an address, in bytes */
char **h_addr_list; /* Null-terminated array of in_addr structs */
};
下面是代码示例。输出地址的格式为digits.digits.digits.digits(其中包含点)
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
struct hostent *he;
struct in_addr a;
int main ()
{
he = gethostbyname ("localhost");
if (he)
{
printf("name: %sn", he->h_name);
while (*he->h_aliases)
printf("alias: %sn", *he->h_aliases++);
while (*he->h_addr_list)
{
bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
printf("address: %sn", inet_ntoa(a));
}
}
else
printf("error");
return 0;
}
输出:name: localhost
address: 127.0.0.1
你想这样做吗:
where Address是char*
struct in_addr IpAddress;
if(inet_aton(Address,&IpAddress))
{
return(ntohl((int)IpAddress.s_addr));
}
.
相关文章:
- Winsock 数据不是从 IP 检索的,而是从普通 URL 检索的
- TCP/IP - 以 C/C++ 格式发送十六进制数据包以控制中继板
- 如何通过TCP/IP Winsock发送双型数据
- 如何在NS3中设置节点的IP地址和数据?
- 我们如何从BOOST :: ASIO :: TCP :: ip :: read_some呼叫中顺序接收多个数据
- 如何避免通过TCP/IP SSH隧道在数据传输中重复读取数据
- 如何避免与"asio::ip::tcp::iostream"的数据竞争?
- 使用C++将完整的以太网数据包发送到特定的ip
- 是否可以根据C/C 代码上的Linux上的(动态更改的IP地址集)丢弃传入数据包
- 获取 PCAP 文件中数据包的 IP 地址
- Winsock2 tcp/ip-一些数据包被忽略,可能是由于前一个数据包的null终止符
- 从 streambuf 使用 boost::asio::ip::tcp 将数据部分写入 TCP 套接字
- Solaris 10 -- 从 recvmsg() 的 UDP 组播数据包中获取 IP 源地址
- TCP/IP IOCP接收到的数据有时已损坏-Windows上的Visual C++
- 使用 IOCP 的 TCP/IP 服务器.接收缓冲区中偶尔的数据损坏
- 原始数据包创建导致 IP 字段顺序不正确
- 如何在WinSock2中的DGRAM套接字上获取传入数据包的IP地址
- 如何修改数据报原始TCP/IP
- UDP数据包到LAN中的特定IP
- 使用gethostbyname()提取IP数据