-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInetAddress.cpp
More file actions
31 lines (31 loc) · 944 Bytes
/
Copy pathInetAddress.cpp
File metadata and controls
31 lines (31 loc) · 944 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
#include "InetAddress.hpp"
#include <strings.h>
#include<string.h>
InetAddress::InetAddress(uint16_t port, string ip){
bzero(&_addr, sizeof _addr);
_addr.sin_family = AF_INET;
_addr.sin_port = htons(port); //将port转成网络字节序
_addr.sin_addr.s_addr = inet_addr(ip.c_str()); //将ip转成网络字节序存储起来
}
string InetAddress::toIP() const{
char buf[64] = {0};
inet_ntop(AF_INET, &_addr.sin_addr, buf, sizeof buf);
return buf;
}
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;
}
uint16_t InetAddress::toPort() const{
return ntohs(_addr.sin_port);
}
const sockaddr_in InetAddress::getSockaddr_in() const{
return _addr;
}
void InetAddress::setAddr(const sockaddr_in& addr){
_addr = addr;
}