1 Star 0 Fork 0

迟来的grown/linux

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
inetaddr.hpp 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
迟来的grown 提交于 2025-09-10 21:19 +08:00 . udp-dict/chat
#pragma once
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
namespace hladdr
{
class InetAddr
{
public:
InetAddr() {}
InetAddr(std::string ip, uint16_t port)
: _ip(ip), _port(port)
{
_addr.sin_family = AF_INET;
inet_aton(_ip.c_str(), &_addr.sin_addr);
_addr.sin_port = htons(_port);
}
InetAddr(struct sockaddr_in addr) : _addr(addr)
{
_ip = inet_ntoa(_addr.sin_addr);
_port = ntohs(addr.sin_port);
}
InetAddr(uint16_t port) : _ip("0"), _port(port)
{
_addr.sin_family = AF_INET;
_addr.sin_addr.s_addr = INADDR_ANY;
_addr.sin_port = htons(_port);
}
struct sockaddr *GetInetAddr() { return (struct sockaddr *)&_addr; }
std::string GetIP() { return _ip; }
uint16_t GetPort() { return _port; }
socklen_t GetLen() { return sizeof(_addr); }
bool operator==(InetAddr &addr) { return _ip == addr._ip && _port == addr._port; }
std::string ToString() { return _ip + "-" + std::to_string(_port); }
void Set(std::string ip, uint16_t port)
{
_ip = ip;
_port = port;
_addr.sin_family = AF_INET;
inet_aton(_ip.c_str(), &_addr.sin_addr);
_addr.sin_port = htons(_port);
}
private:
struct sockaddr_in _addr;
std::string _ip;
uint16_t _port;
};
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/lxb0720/linux.git
git@gitee.com:lxb0720/linux.git
lxb0720
linux
linux
master

搜索帮助