SpiecsEngine
 
Loading...
Searching...
No Matches
InetAddress.cpp
Go to the documentation of this file.
1/**
2* @file InetAddress.h.
3* @brief The InetAddress Class Implementation.
4* @author Spices & Muduo.
5*/
6
7#include "Pchheader.h"
8#include "InetAddress.h"
9
10#include <ws2tcpip.h>
11
12namespace Spices {
13
14namespace Net {
15
16 InetAddress::InetAddress(uint16_t port, std::string ip)
17 {
19
20 memset(&m_Address, 0, sizeof(m_Address));
21
22 m_Address.sin_family = AF_INET;
23 m_Address.sin_port = htons(port);
24 m_Address.sin_addr.S_un.S_addr = inet_addr(ip.c_str());
25 }
26
27 std::string InetAddress::ToIP() const
28 {
30
31 char buf[64] = { 0 };
32 ::inet_ntop(AF_INET, &m_Address.sin_addr, buf, sizeof(buf));
33 return buf;
34 }
35
36 std::string InetAddress::ToIPPort() const
37 {
39
40 char buf[64] = { 0 };
41 ::inet_ntop(AF_INET, &m_Address.sin_addr, buf, sizeof(buf));
42 const size_t end = strlen(buf);
43 const uint16_t port = ntohs(m_Address.sin_port);
44 sprintf(buf + end, ":%u", port);
45 return buf;
46 }
47
48 uint16_t InetAddress::ToPort() const
49 {
51
52 return ntohs(m_Address.sin_port);
53 }
54}
55
56}
#define SPICES_PROFILE_ZONE
std::string ToIPPort() const
Get IP and Port from Socket Address.
std::string ToIP() const
Get IP from Socket Address.
uint16_t ToPort() const
Get Port and Port from Socket Address.
InetAddress(uint16_t port=0, std::string ip="127.0.0.1")
Constructor Function.
This class is Wrapper of current socket address.
Definition InetAddress.h:22