Compare commits
No commits in common. "main" and "cui" have entirely different histories.
5
.vscode/extensions.json
vendored
5
.vscode/extensions.json
vendored
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"recommendations": [
|
|
||||||
"ms-vscode.cmake-tools"
|
|
||||||
]
|
|
||||||
}
|
|
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"files.associations": {
|
|
||||||
"functional": "cpp"
|
|
||||||
}
|
|
||||||
}
|
|
9
main.cc
9
main.cc
@ -2,15 +2,8 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void output(){ //循环输出1-100
|
|
||||||
for(int i = 1; i <= 100; i++){
|
|
||||||
cout << i << endl;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
cout << "100" << endl;
|
|
||||||
cout << "hello world!" << endl;
|
cout << "hello world!" << endl;
|
||||||
cout << "hello too!" << endl;
|
cout << "hello too!" << endl;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,72 +0,0 @@
|
|||||||
|
|
||||||
/*
|
|
||||||
muduo网络库给用户提供了两个主要的类
|
|
||||||
TcpServer :用于编写服务器程序的
|
|
||||||
TcpClient :用于编写客户端程序的
|
|
||||||
|
|
||||||
epoll+线程池
|
|
||||||
好处 :能够把网络I/O的代码和业务代码区分开
|
|
||||||
用户的连接和断开 用户的可读写事件
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <muduo/net/TcpServer.h>
|
|
||||||
#include <muduo/net/EventLoop.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include <functional>
|
|
||||||
using namespace std;
|
|
||||||
using namespace muduo;
|
|
||||||
using namespace muduo::net;
|
|
||||||
using namespace placeholders;
|
|
||||||
|
|
||||||
/*
|
|
||||||
基于muduo网络库开发服务器程序
|
|
||||||
1.组合TcpServer对象
|
|
||||||
2.创建事件循环对象的指针
|
|
||||||
3.明确TcpServer构造函数需要什么参数,输出ChatServer的构造函数
|
|
||||||
4.在当前服务器类的构造函数当中,注册处理连接的回调函数和处理读写事件的回调函数
|
|
||||||
5.设置合适的服务端线程数量,muduo库会自己分配I/O线程和worker线程
|
|
||||||
*/
|
|
||||||
|
|
||||||
class ChatServer{
|
|
||||||
public:
|
|
||||||
ChatServer(EventLoop* loop,
|
|
||||||
const InetAddress& listenAddr,
|
|
||||||
const string& nameArg)
|
|
||||||
:_server(loop,listenAddr,nameArg),_loop(loop)
|
|
||||||
{
|
|
||||||
//给服务器注册用户连接的创建和断开回调
|
|
||||||
_server.setConnectionCallback(std::bind(&ChatServer::onConnection, this, _1));
|
|
||||||
|
|
||||||
//给服务器注册用户读写事件的回调
|
|
||||||
_server.setMessageCallback(std::bind(&ChatServer::onMessage, this, _1, _2, _3));
|
|
||||||
|
|
||||||
//设置服务器端的线程数量 1个I/O线程 3个worker线程
|
|
||||||
_server.setThreadNum(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
void start(){
|
|
||||||
_server.start();
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
void onConnection(const TcpConnectionPtr&)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void onMessage(const TcpConnectionPtr& conn,
|
|
||||||
Buffer* buffer,
|
|
||||||
Timestamp time)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
TcpServer _server;
|
|
||||||
EventLoop* _loop;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user