-
Notifications
You must be signed in to change notification settings - Fork 0
/
InetAddress.cc
46 lines (39 loc) · 1.48 KB
/
InetAddress.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "InetAddress.h"
#include <strings.h>
#include <string.h>
InetAddress::InetAddress(uint16_t port, std::string ip)
{
bzero(&addr_, sizeof addr_); // 将 addr_ 清零
addr_.sin_family = AF_INET; // IPv4
addr_.sin_port = htons(port); // 端口号
addr_.sin_addr.s_addr = inet_addr(ip.c_str()); // IP 地址
}
std::string InetAddress::toIp() const
{
// The :: before the function name indicates that it is a global function and not a member function of a class.
char buf[64] = {0};
::inet_ntop(AF_INET, &addr_.sin_addr, buf, sizeof buf); // 将网络字节序转换为字符串
return buf;
}
std::string InetAddress::toIpPort() const
{
// 字符串形式的ip:port
// The :: before the function name indicates that it is a global function and not a member function of a class.
char buf[64] = {0};
::inet_ntop(AF_INET, &addr_.sin_addr, buf, sizeof buf); // 将网络字节序转换为字符串
size_t end = strlen(buf); // 字符串的长度
uint16_t port = ntohs(addr_.sin_port); // 端口号
sprintf(buf + end, ":%u", port); // 将端口号加到字符串的末尾
return buf;
}
uint16_t InetAddress::toPort() const
{
return ntohs(addr_.sin_port); // 端口号
}
// #include <iostream>
// int main()
// {
// InetAddress addr(8080);
// std::cout << addr.toIpPort() << std::endl;
// return 0;
// }