-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInetAddress.cpp
More file actions
36 lines (28 loc) · 852 Bytes
/
Copy pathInetAddress.cpp
File metadata and controls
36 lines (28 loc) · 852 Bytes
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
#include "InetAddress.h"
#include "SocketOps.h"
#include <string.h> // for memset
#include <arpa/inet.h> // for inet_addr
InetAddress::InetAddress(uint16_t port, const std::string& ip) {
memset(&addr_, 0, sizeof(addr_));
addr_.sin_family = AF_INET;
addr_.sin_port = htons(port);
inet_pton(AF_INET, ip.c_str(), &addr_.sin_addr);
}
InetAddress::InetAddress(const sockaddr_in& addr)
: addr_(addr) {}
const sockaddr_in* InetAddress::getSockAddr() const {
return &addr_;
}
void InetAddress::setSockAddr(const sockaddr_in& addr) {
addr_ = addr;
}
std::string InetAddress::toIp() const {
char buf[64] = "";
sockets::toIp(buf, sizeof(buf), getSockAddr());
return buf;
}
std::string InetAddress::toIpPort() const {
char buf[64] = "";
sockets::toIpPort(buf, sizeof(buf), getSockAddr());
return buf;
}