/**********************server start*********************/
//server.h
#ifndef SERVER_H
#define SERVER_H
#include <QThread>
#include <QDebug>
#include "muduo/base/Logging.h"
#include "TcpServer.h"
#include "EventLoop.h"
#include "InetAddress.h"
#include <functional>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
using namespace muduo;
using namespace muduo::net;
using namespace std;
class Server : public QThread
{
Q_OBJECT
public:
explicit Server();
void run()override;
void Start(int port=8080);
void Stop();
void Send(string data);
void onConnection(const TcpConnectionPtr& conn);
void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp time);
signals:
void cppSigRecv(QString data);
public slots:
private:
EventLoop* loop_ = nullptr;
TcpServer *server_ = nullptr;
int port;
};
//server.cpp
#include "server.h"
Server::Server()
{
}
void Server::onConnection(const TcpConnectionPtr &conn)
{
qDebug() << conn->peerAddress().toIpPort().c_str() << " -> "
<< conn->localAddress().toIpPort().c_str() << " is "
<< (conn->connected() ? "UP" : "DOWN");
//conn->send("hello calm\n");
}
void Server::onMessage(const TcpConnectionPtr &conn, Buffer *buf, Timestamp time)
{
string msg(buf->retrieveAllAsString());
//LOG_INFO << conn->name() << " recv " << msg.size() << " bytes at " << time.toString();
LOG_INFO << "server recv: " << msg;
if (msg == "exit\n")
{
conn->send("bye\n");
conn->shutdown();//关闭写端
}
else if (msg == "quit\n")
{
loop_->quit();
}
else
emit cppSigRecv(QString::fromStdString(msg));
//conn->send(msg);
}
void Server::run()
{
qDebug() << "run";
loop_ = new EventLoop();
InetAddress serverAddr("0.0.0.0", port);
server_ = new TcpServer(loop_, serverAddr, "Server");
//给服务器注册用户建立断开连接的回调
server_->setConnectionCallback(std::bind(&Server::onConnection,this,std::placeholders::_1));
//给服务器注册用户可读写事件回调
server_->setMessageCallback(std::bind(&Server::onMessage,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));
//server_->setThreadNum(3);
server_->start();
loop_->loop();
LOG_INFO << "exit";
}
void Server::Start(int port)
{
this->port = port;
start();
}
void Server::Stop()
{
if(loop_)
loop_->quit();
}
void Server::Send(string data)
{
std::map<string, TcpConnectionPtr> map = server_->connection();
std::map<std::string, TcpConnectionPtr>::iterator it;
for (it = map.begin(); it != map.end(); it++)
{
LOG_INFO << it->first;
it->second->send(data);
}
}
/*********************server end*******************************/
/*********************client end*******************************/
//.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QThread>
#include <QDebug>
#include "muduo/base/Logging.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/InetAddress.h"
#include "muduo/net/TcpClient.h"
#include <utility>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
using namespace muduo;
using namespace muduo::net;
using namespace std;
class Client : public QThread
{
Q_OBJECT
public:
Client();
#if 0
Client(EventLoop* loop, const InetAddress& listenAddr)
: loop_(loop),
client_(new TcpClient(loop, listenAddr, "Client"))
{
client_->setConnectionCallback(
std::bind(&Client::onConnection, this, _1));
client_->setMessageCallback(
std::bind(&Client::onMessage, this, _1, _2, _3));
// client_.enableRetry();
}
#endif
void connect()
{
client_->connect();
}
void Start(string ip="127.0.0.1", int port=8080);
void Stop();
void Send(string data);
signals:
void cppSigRecv(QString data);
private:
void onConnection(const TcpConnectionPtr& conn);
void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp receiveTime);
void run()override;
EventLoop* loop_ = nullptr;
TcpClient *client_ = nullptr;
string ip;
int port;
};
#endif // CHARGENCLIENT_H
//client.cpp
#include "client.h"
Client::Client()
{
}
void Client::onConnection(const TcpConnectionPtr& conn)
{
cout << conn->localAddress().toIpPort() << " -> "
<< conn->peerAddress().toIpPort() << " is "
<< (conn->connected() ? "UP" : "DOWN") << endl;
if (!conn->connected())
loop_->quit();
}
void Client::onMessage(const TcpConnectionPtr &conn, Buffer *buf, Timestamp receiveTime)
{
//buf->retrieveAll();
string msg(buf->retrieveAllAsString());
//LOG_INFO << conn->name() << " recv " << msg.size() << " bytes at " << time.toString();
std::cout << "clent receive: " << msg << endl;
emit cppSigRecv(QString::fromStdString(msg));
}
void Client::run()
{
qDebug() << "run";
loop_ = new EventLoop();
InetAddress serverAddr(ip, port);
client_ = new TcpClient(loop_, serverAddr, "Client");
client_->setConnectionCallback(std::bind(&Client::onConnection, this, _1));
client_->setMessageCallback(std::bind(&Client::onMessage, this, _1, _2, _3));
client_->connect();
loop_->loop();
qDebug() << "exit";
}
void Client::Start(string ip, int port)
{
this->ip = ip;
this->port = port;
start();
}
void Client::Stop()
{
if(loop_)
loop_->quit();
}
void Client::Send(string data)
{
client_->connection()->send(data);
}
#endif // SERVER_H
运行效果如下:
源码地址:点击跳转
觉得有帮助的话,打赏一下呗。。