当前位置:网站首页>Muduo simple usage
Muduo simple usage
2022-06-23 01:39:00 【For financial freedom!】
#include <muduo/net/TcpServer.h>
#include <muduo/net/EventLoop.h>
#include <iostream>
#include <functional>
#include <string>
using namespace std;
using namespace muduo;
using namespace muduo::net;
using namespace placeholders;
/* 1. Combine TcpServer object 2. establish EventLoop Pointer to the time loop object 3. clear TcpServer What parameters do constructors need , Output ChatServer Constructor for 4. In the constructor of the current server class , Register the callback function to handle the connection and the callback function to handle the read-write event 5. Set the appropriate number of server threads ,muduo The library will divide itself i/o Threads and worker Threads */
class ChatServer
{
public:
ChatServer(EventLoop* loop,// The event loop
const InetAddress& listenAddr,//IP+port
const string& nameArg)// Name of the server
:_server(loop,listenAddr,nameArg),_loop(loop)
{
// Register the server User connection creation and To break off Callback
_server.setConnectionCallback(std::bind(&ChatServer::onConnection,this,_1));
// Register user read / write event callback for the server
_server.setMessageCallback(std::bind(&ChatServer::onMessage,this,_1,_2,_3));
// Set the number of threads on the server side 1 individual i/o Threads 3 individual worker Threads
_server.setThreadNum(4);
}
// Turn on the event loop
void start()
{
_server.start();
}
private:
// Dedicated to dealing with user connection creation and disconnection epoll listenfd accept
void onConnection(const TcpConnectionPtr& conn)
{
if(conn->connected())
{
cout << conn->peerAddress().toIpPort() << "->"
<< conn->localAddress().toIpPort()
<< " state:online" << endl;
}
else{
cout << conn->peerAddress().toIpPort() << "->"
<< conn->localAddress().toIpPort()
<< " state:offline" << endl;
conn->shutdown();//close(fd);
//_loop->quit();
}
}
// Specifically handle user read and write events
void onMessage(const TcpConnectionPtr& conn,// Connect
Buffer* buffer,// buffer
Timestamp time)// Time information of receiving data
{
string buf = buffer->retrieveAllAsString();
cout << "recv data:" << buf << "time:" << time.toString() << endl;
conn->send(buf);
}
TcpServer _server;
EventLoop* _loop;
};
int main()
{
EventLoop loop;//epoll
InetAddress addr("127.0.0.1",6000);
ChatServer server(&loop,addr,"CharServer");
server.start();//listenfd adopt epoll_ctl Add to epoll object
loop.loop();//epoll_wait Waiting for new users to connect in a blocked way , Read / write events of connected users, etc
return 0;
}

Enter to execute :


边栏推荐
- C. Number of Pairs-Codeforces Round #725 (Div. 3)
- three. JS simulated driving tour art exhibition hall - creating super camera controller
- Unit of RMB in words
- Lexical Sign Sequence
- 人民币的单位的大写
- Openvino CPU acceleration survey
- Day367: valid complete square
- Phantomjs Usage Summary
- C# SerializableDictionary序列化/反序列化
- JS - single sign on
猜你喜欢

Day367: valid complete square

Network module packaging
![[learning notes] roll back Mo team](/img/19/d374dd172b9609a3f57de50791b19e.png)
[learning notes] roll back Mo team

Byte order: big endian vs little endian

Detailed explanation of clip attribute parameters

E-R diagram

Overview of visual object detection technology based on deep learning

office2016+visio2016

The devil cold rice # 099 the devil said to travel to the West; The nature of the boss; Answer the midlife crisis again; Specialty selection

MySQL-Seconds_ behind_ Master accuracy error
随机推荐
SQL programming task05 job -sql advanced processing
Prevent others from using the browser to debug
B tree and b+ tree
Day367: valid complete square
[hdu] p2087 cut cloth strip
Extend your kubernetes API using the aggregation API
Vector 6 (inheritance)
OOP multiple storage (class template)
three. JS simulated driving tour art exhibition hall - creating super camera controller
Zabbix5 series - use temperature and humidity sensor to monitor the temperature and humidity of the machine room (XX)
Install MySQL (5.7+8.0) through docker and configure master-slave replication (gtid+ enhanced semi synchronization)
JS - single sign on
C serializabledictionary serialization / deserialization
Debian10 LVM logical volumes
Binary String
C. Diluc and Kaeya——Codeforces Round #724 (Div. 2)
Knowledge point learning
[22 summer reconstruction 1] codeworks round 791 (Div. 2)
Centos7 installing postgresql12
[initial launch] there are too many requests at once, and the database is in danger