1 Star 0 Fork 0

周不才/cpp_linux study

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
InetAddr.hpp 1.15 KB
Copy Edit Raw Blame History
周不才 authored 2025-03-13 14:38 +08:00 . RemoteCommandExecution
#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
class InetAddr
{
public:
//构造函数
InetAddr(const struct sockaddr_in &addr):_addr(addr)
{
ToHost(addr);
}
//返回IP地址
std::string Ip()
{
return _ip;
}
//返回端口号
uint16_t Port()
{
return _port;
}
//返回IP地址+端口号
struct sockaddr_in Addr()
{
return _addr;
}
//返回IP地址+端口号的字符串形式
std::string AddrStr()
{
return _ip+" : "+std::to_string(_port);
}
//==运算符重载
bool operator==(const InetAddr& addr)
{
return (this->_ip==addr._ip&&this->_port==addr._port);
}
//析构函数
~InetAddr()
{
}
private:
void ToHost(const struct sockaddr_in &addr)
{
_port=ntohs(addr.sin_port);//端口号网络字节序转主机字节序
_ip=inet_ntoa(addr.sin_addr);//IP地址网络字节序转点分十进制字符串
}
private:
uint16_t _port;
std::string _ip;
struct sockaddr_in _addr;
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/yan0605/cpp_linux-study.git
git@gitee.com:yan0605/cpp_linux-study.git
yan0605
cpp_linux-study
cpp_linux study
master

Search