-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInetAddress.cc
34 lines (28 loc) · 878 Bytes
/
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
#include "InetAddress.h"
#include <cstring>
InetAddress::InetAddress(uint16_t port, std::string ip) {
bzero(&addr_, sizeof(addr_));
addr_.sin_family = AF_INET;
addr_.sin_port = htons(port);
addr_.sin_addr.s_addr = inet_addr(ip.c_str());
}
InetAddress::InetAddress(const sockaddr_in &addr) : addr_(addr) { }
std::string InetAddress::toIp() const {
char buf[64] = {0};
::inet_ntop(AF_INET, &addr_.sin_addr, buf, sizeof(buf));
return buf;
}
uint16_t InetAddress::toPort() const {
return ntohs(addr_.sin_port);
}
std::string InetAddress::toIpPort() const {
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;
}
const sockaddr_in* InetAddress::getSockAddr() const {
return &addr_;
}