当前位置:网站首页>[Muduo socket] InetAddress encapsulates the socket address type
[Muduo socket] InetAddress encapsulates the socket address type
2022-07-21 02:40:00 【Cx330( ͡ _ ͡ °)】
When parsing the source code : First understand the core code , You don't need to know about branches first , Reading line by line is easy to get confused
First of all, the log is finished , Let's see TcpServer, TcpServer Namely muduo A class of program entry of Network Library . Then combine this class , Let's look at its constructor :
- EventLoop *loop : Event The event loop , The larger , Just leave it alone
- const InetAddress& listenAddr :IP Address and port number encapsulate classes , As TcpServer The parameters of the constructor of the object are passed in . This class is relatively small . You can deal with it first
- const string& nameArg :
- Option option = kNoReusePort :
encapsulation SOCKET Address type :ipv4,Ipv6
We only package ipv4
Member functions :
explicit InetAddress(uint16_t port, std::string ip = "127.0.0.1") : Constructor passes in the port number and IP Address , Then encapsulate the address port
explicit InetAddress(const sockaddr_in &addr): addr_(addr) { } : Direct in sockaddr Of sock Address
std::string toIp() const; obtain IP Information
std::string toIpPort() const; obtain IP、PORT Information
uint64_t toPort() const; Get the port number
const sockaddr_in* getSockaddr() const : Get member variables
Member variables :
sockaddr_in addr_; sock Address
//InetAddress.h
#pragma once
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string>
class InetAddress
{
public:
explicit InetAddress(uint16_t port, std::string ip = "127.0.0.1");
explicit InetAddress(const sockaddr_in &addr): addr_(addr) { }
std::string toIp() const;
std::string toIpPort() const;
uint64_t toPort() const;
const sockaddr_in* getSockaddr() const { return &addr_; }
private:
sockaddr_in addr_;
};
//InetAddress.cc
#include "InetAddress.h"
#include <strings.h>
#include <string.h>
InetAddress::InetAddress(uint16_t port, std::string ip)
{
bzero(&addr_, sizeof addr_); // Zero clearing
addr_.sin_family = AF_INET;
addr_.sin_port = htons(port);
// Convert a string into an integer ip Address , Then turn to network byte order
addr_.sin_addr.s_addr = inet_addr(ip.c_str());
}
std::string InetAddress::toIp() const
{
// Because we have converted the network byte order , Need to reverse local byte order
char buf[64] = {0};
::inet_ntop(AF_INET, &addr_.sin_addr, buf, sizeof(buf));
return buf;
}
std::string InetAddress::toIpPort() const
{
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;
}
uint64_t InetAddress::toPort() const
{
return ntohs(addr_.sin_port);
}
// #include <iostream>
// int main()
// {
// InetAddress addr(8080);
// std::cout << addr.toIpPort() << std::endl;
// return 0;
// }
边栏推荐
- 得物App数据模拟平台的探索和实践
- Thread pool code and testing
- 买量洞察与渠道评估,助力营销决策优化
- 织梦DEDE后台系统用户管理显示空白的解决方法
- To get to the bottom: Principle Analysis of Objective-C correlation attribute
- Construction and practice of full stack code test coverage and use case discovery system
- 【MUDUO】构建项目编译CMake文件以及noncopyable
- Fiddler5+ lightning simulator 4.0 settings for app packet capturing
- Metauniverse 3D wilderness
- Real time debugging practice based on attach to process
猜你喜欢
How does excel perform multi criteria search? Method summary of Excel multi condition search function
Excel怎么转换为Word格式?将Excel转换为Word格式的方法
得物App数据模拟平台的探索和实践
我把整个研发中台拆分过程的一些心得总结
EF core learning notes: additional foreign key attributes / single navigation attributes
软件界面和简单系统仿真
Interpreting the implementation principle of go Distributed Link Tracking
图解LeetCode——731. 我的日程安排表 II(难度:中等)
Apache Flink 的 YARN Session 提交流程
织梦DEDE后台系统用户管理显示空白的解决方法
随机推荐
Excel宏是什么?Excel宏的使用教程
Check whether the date is valid
Analysis on the wallet system architecture of Baidu trading platform
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Date从对象中获取工作日的名称
What is Excel Macro? Tutorial on using Excel macros
计算从今天开始的天数
Zhimeng adds a column and prompts "failed to save the catalog data, please check whether there is a problem with your input data" when modifying the column
大佬们问个问题,flinksql写入mysql,非空字段写入不进去,报我没给默认值咋回事,有大佬看看
【FAQ】接入HMS Core推送服务,服务端下发消息常见错误码原因分析及解决方法
返回日期对应的一年中的第几周
One article speed learning - basis of pyspark data analysis: detailed explanation of pyspark basic functions and basic grammar
检查日期是否在其他两个日期之间
Interpreting the implementation principle of go Distributed Link Tracking
Excel怎么转换为Word格式?将Excel转换为Word格式的方法
Fiddler5+ lightning simulator 4.0 settings for app packet capturing
怎么批量删除Excel电子表格中不同的文字?
【MUDUO SOCKET】InetAddress 封装SOCKET地址类型
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Huawei's general card identification function enables multiple card bindings with one key