12 Star 42 Fork 2

whb-helloworld/112

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
InetAddr.hpp 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
gitTest-比特科技 提交于 2024-12-02 21:54 +08:00 . udp socket
#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Common.hpp"
class InetAddr
{
private:
void PortNet2Host()
{
_port = ::ntohs(_net_addr.sin_port);
}
void IpNet2Host()
{
char ipbuffer[64];
const char *ip = ::inet_ntop(AF_INET, &_net_addr.sin_addr, ipbuffer, sizeof(ipbuffer));
(void)ip;
}
public:
InetAddr()
{
}
InetAddr(const struct sockaddr_in &addr) : _net_addr(addr)
{
PortNet2Host();
IpNet2Host();
}
InetAddr(uint16_t port) : _port(port), _ip("")
{
_net_addr.sin_family = AF_INET;
_net_addr.sin_port = htons(_port);
_net_addr.sin_addr.s_addr = INADDR_ANY;
}
struct sockaddr *NetAddr() { return CONV(&_net_addr); }
socklen_t NetAddrLen() { return sizeof(_net_addr); }
std::string Ip() { return _ip; }
uint16_t Port() { return _port; }
~InetAddr()
{
}
private:
struct sockaddr_in _net_addr;
std::string _ip;
uint16_t _port;
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/whb-helloworld/112.git
git@gitee.com:whb-helloworld/112.git
whb-helloworld
112
112
master

搜索帮助