这个协议有“2038 年问题”。服务端只需要关注“三个半事件”中的“连接已建立”事件,事件处理函数如下:
1: void TimeServer::onConnection(const muduo::net::TcpConnectionPtr& conn) 2: { 3: LOG_INFO << "TimeServer - " << conn->peerAddress().toHostPort() << " -> " 4: << conn->localAddress().toHostPort() << " is " 5: << (conn->connected() "UP" : "DOWN"); 6: if (conn->connected()) 7: { 8: int32_t now = sockets::hostToNetwork32(static_cast(::time(NULL))); 9: conn->send(&now, sizeof now); // 发送 4 个字节 10: conn->shutdown(); // 主动断开连接 11: } 12: }剩下的都是例行公事的代码,为节省篇幅,此处从略,请阅读 muduo/examples/simple/time。
用 netcat 扮