5: 尽量避免程序退出时有没有close的socket。
#pragma once #include <functional> namespace Hi { /* * @ brief TCP监听会发送的通知 */ class TcpEvent { public: /* brief 客户端连接成功通知, * 参数分别为socket描述符,远端ip,远端端口,本地ip,本地端口 */ std::function<void(int, const char*, unsigned short, const char*, unsigned short)> on_open_; /* brief 客户端连接断开通知 */ std::function<void(int)> on_close_; /* brief 接收到客户端消息通知 */ std::function<void(int)> on_receive_; }; /* * @ brief TCP监听类 */ class HiTcpServer { public: /* * @brief 启动监听 * @param [in] evt 通知对象 * @param [in] port 简体端口 * @param [in] ip 监听IP,如果为空时,表示监听本机所有的IP * @retval true:成功;false:失败 */ bool open(const TcpEvent& evt, unsigned short port, const char* ip = NULL); /* * @brief 停止监听 * @retval true:成功;false:失败 */ bool close(); }; }
#pragma once #include <string> #include <sys/socket.h> #include <iostream> #include "net/hiTcpServer.h" using namespace std; // 客户端连接成功 static void on_client_open(int sock, const char* remote_ip, unsigned short remote_port, const char* local_ip, unsigned short local_port) { cout<<"accept a client connect,socket["<< sock<<"] remote["<<remote_ip<<","<<remote_port<< "]local["<<local_ip<<","<<local_port<<"]"<<endl; } // 接收到客户端消息 static void on_client_recv_data(int sock) { char rece_buf[256]; memset(rece_buf, 0, 256); int n = recv(sock, rece_buf, 256, 0); cout<<"receive client(socket:"<<sock<<") data,len:"<<n<<endl; } // 客户端连接断开 static void on_client_close(int sock) { cout<<"client (socket:"<<sock<<") is close"<<endl; } void main() { Hi::TcpEvent evt; evt.on_open_ = std::bind(&on_client_open); evt.on_close_ = std::bind(&on_client_close); evt.on_receive_ = std::bind(&on_client_recv_data); Hi::HiTcpServer server; server.open(evt, 6000); sleep(60); }
/* * @brief socket通道信息类 */ class SocketChannel { public: SocketChannel(): sock_(-1), remote_port_(0), local_port_(0) { } public: int sock_; ///< socket文件描述符 std::string remote_ip_; ///< 对端IP unsigned short remote_port_; ///< 对端端口 std::string local_ip_; ///< 本地IP unsigned short local_port_; ///< 本地端口 };
#pragma once #include <functional> namespace Hi { /* * @brief socket通道信息类 */ class SocketChannel { public: SocketChannel(): sock_(-1), remote_port_(0), local_port_(0) { } public: int sock_; ///< socket文件描述符 std::string remote_ip_; ///< 对端IP unsigned short remote_port_; ///< 对端端口 std::string local_ip_; ///< 本地IP unsigned short local_port_; ///< 本地端口 }; /* * @ brief TCP监听会发送的通知 */ class TcpEvent { public: /* brief 客户端连接成功通知 */ std::function<void(int, SocketChannel&)> on_open_; /* brief 客户端连接断开通知 */ std::function<void(int)> on_close_; /* brief 接收到客户端消息通知 */ std::function<void(int)> on_receive_; }; /* @ brief 逻辑实现类*/ class TcpServerImpl; /* * @ brief TCP监听类 */ class HiTcpServer { public: HiTcpServer(); ~HiTcpServer(); HiTcpServer& operator =(const HiTcpServer&) = delete; HiTcpServer(const HiTcpServer&) = delete; public: /* * @brief 启动监听 * @param [in] evt 通知对象 * @param [in] port 监听端口 * @param [in] ip 监听IP,如果为空时,表示监听本机所有的IP * @retval true:成功;false:失败 */ bool open(const TcpEvent& evt, unsigned short port, const char* ip = NULL); /* * @brief 停止监听 * @retval true:成功;false:失败 */ bool close(); private: TcpServerImpl* impl_; /* brief 实现逻辑的指针 */ }; }
#pragma once #include <functional> #include <string> #include <netinet/in.h> namespace Hi { /* * @brief socket通道信息类 */ class SocketChannel { public: SocketChannel(): sock_(-1), remote_port_(0), local_port_(0) { } public: int sock_; ///< socket文件描述符 std::string remote_ip_; ///< 对端IP unsigned short remote_port_; ///< 对端端口 std::string local_ip_; ///< 本地IP unsigned short local_port_; ///< 本地端口 }; /* * @ brief TCP监听会发送的通知 */ class TcpEvent { public: TcpEvent(); public: /* brief 客户端连接成功通知 */ std::function<void(int, SocketChannel&)> on_open_; /* brief 客户端连接断开通知 */ std::function<void(int)> on_close_; /* brief 接收到客户端消息通知 */ std::function<void(int)> on_receive_; }; }
#pragma once #include "net/hiNetCommon.h" namespace Hi { /* @ brief 逻辑实现类*/ class TcpServerImpl; /* * @ brief TCP监听类 */ class HiTcpServer { public: HiTcpServer(); ~HiTcpServer(); HiTcpServer& operator =(const HiTcpServer&) = delete; HiTcpServer(const HiTcpServer&) = delete; public: /* * @brief 启动监听 * @param [in] evt 通知对象 * @param [in] port 监听端口 * @param [in] ip 监听IP,如果为空时,表示监听本机所有的IP * @retval true:成功;false:失败 */ bool open(const TcpEvent& evt, unsigned short port, const char* ip = NULL); /* * @brief 停止监听 * @retval true:成功;false:失败 */ bool close(); private: TcpServerImpl* impl_; /* brief 实现逻辑的指针 */ }; }
#pragma once #include <string> #include <sys/socket.h> #include <iostream> #include "net/hiTcpServer.h" using namespace std; // 客户端连接成功 static void on_client_open(int sock, Hi::SocketChannel& channel) { cout<<"accept a client connect,socket["<< sock<<"] remote["<<channel.remote_ip<< ","<<channel.remote_port<< "]local["<<channel.local_ip<<","<< channel.local_port<<"]"<<endl; } // 接收到客户端消息 static void on_client_recv_data(int sock) { char rece_buf[256]; memset(rece_buf, 0, 256); int n = recv(sock, rece_buf, 256, 0); cout<<"receive client(socket:"<<sock<<") data,len:"<<n<<endl; } // 客户端连接断开 static void on_client_close(int sock) { cout<<"client (socket:"<<sock<<") is close"<<endl; } void main() { Hi::TcpEvent evt; evt.on_open_ = std::bind(&on_client_open); evt.on_close_ = std::bind(&on_client_close); evt.on_receive_ = std::bind(&on_client_recv_data); Hi::HiTcpServer server; server.open(evt, 6000); sleep(60); }
我想写一个Linux下的C++程序库--记我的C++库设计历程:设计一个TCP服务程序
原文地址:http://blog.csdn.net/xumingxsh/article/details/43867477