1 Star 0 Fork 0

pulin88 / mymuduo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
InetAddress.cc 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
vmware_centos_pulin 提交于 2022-02-12 01:40 . mymuduo init
#include "InetAddress.h"
#include <string.h>
/*
inet_pton 本地const char* --> 网络uint
inet_ntop 网络uint --> 本地const char*
htons 本地short --> 网络short
ntohs 网络short --> 本地short
*/
InetAddress::InetAddress(uint16_t port, string ip)
{
bzero(&addr_, sizeof addr_);
addr_.sin_family = AF_INET;
addr_.sin_port = htons(port);
inet_pton(AF_INET, ip.c_str(), &addr_.sin_addr);
}
string InetAddress::toIp() const
{
char buf[64] = {0};
inet_ntop(AF_INET, &addr_.sin_addr, buf, 64);
return buf;
}
string InetAddress::toIpPort() const
{
char buf[64] = {0};
inet_ntop(AF_INET, &addr_.sin_addr, buf, 64);
int 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);
}
/*
int main()
{
cout << InetAddress(22,"127.127.127.127").toIpPort() << endl;
cout << InetAddress(22,"127.127.127.127").toPort() << endl;
cout << InetAddress(22,"127.127.127.127").toIp() << endl;
}
*/
C++
1
https://gitee.com/pulin88/mymuduo.git
git@gitee.com:pulin88/mymuduo.git
pulin88
mymuduo
mymuduo
main

搜索帮助