1bfe73648SShuo Chen#include "TcpServer.h" 2bfe73648SShuo Chen#include "EventLoop.h" 3bfe73648SShuo Chen#include "InetAddress.h" 4bfe73648SShuo Chen#include <stdio.h> 5bfe73648SShuo Chen 6bfe73648SShuo Chenvoid onConnection(const muduo::TcpConnectionPtr& conn) 7bfe73648SShuo Chen{ 8bfe73648SShuo Chen if (conn->connected()) 9bfe73648SShuo Chen { 10bfe73648SShuo Chen printf("onConnection(): new connection [%s] from %s\n", 11bfe73648SShuo Chen conn->name().c_str(), 12bfe73648SShuo Chen conn->peerAddress().toHostPort().c_str()); 13bfe73648SShuo Chen } 14bfe73648SShuo Chen else 15bfe73648SShuo Chen { 16bfe73648SShuo Chen printf("onConnection(): connection [%s] is down\n", 17bfe73648SShuo Chen conn->name().c_str()); 18bfe73648SShuo Chen } 19bfe73648SShuo Chen} 20bfe73648SShuo Chen 21bfe73648SShuo Chenvoid onMessage(const muduo::TcpConnectionPtr& conn, 22bfe73648SShuo Chen const char* data, 23bfe73648SShuo Chen ssize_t len) 24bfe73648SShuo Chen{ 25bfe73648SShuo Chen printf("onMessage(): received %zd bytes from connection [%s]\n", 26bfe73648SShuo Chen len, conn->name().c_str()); 27bfe73648SShuo Chen} 28bfe73648SShuo Chen 29bfe73648SShuo Chenint main() 30bfe73648SShuo Chen{ 31bfe73648SShuo Chen printf("main(): pid = %d\n", getpid()); 32bfe73648SShuo Chen 33bfe73648SShuo Chen muduo::InetAddress listenAddr(9981); 34bfe73648SShuo Chen muduo::EventLoop loop; 35bfe73648SShuo Chen 36bfe73648SShuo Chen muduo::TcpServer server(&loop, listenAddr); 37bfe73648SShuo Chen server.setConnectionCallback(onConnection); 38bfe73648SShuo Chen server.setMessageCallback(onMessage); 39bfe73648SShuo Chen server.start(); 40bfe73648SShuo Chen 41bfe73648SShuo Chen loop.loop(); 42bfe73648SShuo Chen} 43