加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
InetAddress.cpp 993 Bytes
一键复制 编辑 原始数据 按行查看 历史
leo_1 提交于 2023-10-22 23:33 . 添加tcpserver相关实现
/***
* @Description:
* @Author: LeoTao
* @Date: 2023-10-03 10:24:40
* @LastEditTime: 2023-10-22 22:28:38
*/
#include "InetAddress.h"
#include <strings.h>
#include <string.h>
InetAddress::InetAddress(uint16_t port, std::string ip){
//清零
bzero(&addr_, sizeof(addr_));
//创建
addr_.sin_family = AF_INET;
addr_.sin_port = htons(port);
addr_.sin_addr.s_addr = inet_addr(ip.c_str());
}
std::string InetAddress::toIp() const{
//从addr_网络字节序中解析出ip地址
char buf[64] = {0};
::inet_ntop(AF_INET,&addr_.sin_addr, buf, sizeof(buf));
return buf;
}
std::string InetAddress::toIpPort() const{
//从addr_网络字节序中解析出ip地址
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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化