My second muduo library server program

· Description: A simple finger server.
• The need to install the library: muduo, the Boost
· After installing the library, directly run the following command (+ compiler run) execute the program:

g++ -o test test.cpp -lmuduo_net -lmuduo_base -lpthread; ./test

test.cpp code is as follows:

#include <muduo/net/EventLoop.h>
#include <muduo/net/TcpServer.h>
#include <muduo/base/Logging.h>

using namespace muduo;
using namespace muduo::net;

void onConnection(const TcpConnectionPtr& conn){
	if (conn->connected()){
		LOG_INFO << "连接建立";
	}else{
		LOG_INFO << "连接断开";
	}
}

void onMessage(const TcpConnectionPtr &conn, Buffer *buf, Timestamp receiveTime){
	if (buf->findCRLF()){
		conn->send("来自服务器的问候,哈哈哈哈哈哈\r\n");
		//conn->shutdown();
	}
}

int main(){
	EventLoop loop;
	TcpServer server(&loop, InetAddress(1079), "Finger");
	server.setConnectionCallback(onConnection);
	server.setMessageCallback(onMessage);
	server.start();
	loop.loop();
}

Results are as follows:
Here Insert Picture Description

telnet test server program (telnet server address port number):

telnet localhost 1079

Test results are as follows:
Here Insert Picture Description


Reference "Linux multi-threaded server-side programming."

Published 92 original articles · won praise 2 · Views 3409

Guess you like

Origin blog.csdn.net/zxc120389574/article/details/105220605