From 2d15bc935edad07c8b1fb0a95eb61946b218475c Mon Sep 17 00:00:00 2001 From: Saturneric Date: Tue, 24 Nov 2020 14:39:43 +0800 Subject: [PATCH] Modified. --- CMakeLists.txt | 11 +++++++++++ include/communicate/tcp.h | 13 +++++++++++++ include/communicate/tcp_client.h | 19 +++++++++---------- include/communicate/tcp_server.h | 18 +++++------------- include/communicate/tcp_session.h | 27 +++++++++++++++++++++++++++ include/project.h | 31 +++++++++++++++++++++++++++++++ src/communicate/tcp_client.cpp | 12 ++++++++---- src/communicate/tcp_server.cpp | 22 ++++++++++++---------- src/communicate/tcp_session.cpp | 5 +++++ test/test_commu/main.cpp | 3 ++- 10 files changed, 123 insertions(+), 38 deletions(-) create mode 100644 include/communicate/tcp.h create mode 100644 include/communicate/tcp_session.h create mode 100644 include/project.h create mode 100644 src/communicate/tcp_session.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c6aa19..9b6c392 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,17 @@ set(CMAKE_CXX_STANDARD 14) include_directories(include/) include_directories(utils/) + + +IF (WIN32) + +ELSEIF (APPLE) + set(OPENSSL_ROOT_DIR /usr/local/opt/openssl@1.1) + set(OPENSSL_LIBRARIES /usr/local/opt/openssl@1.1/lib) +ELSEIF (UNIX) + +ENDIF () + find_package(Boost REQUIRED thread) find_package(OpenSSL 1.1.1 REQUIRED) diff --git a/include/communicate/tcp.h b/include/communicate/tcp.h new file mode 100644 index 0000000..f545b45 --- /dev/null +++ b/include/communicate/tcp.h @@ -0,0 +1,13 @@ +// +// Created by 胡宇 on 2020/10/23. +// + +#ifndef NET_TCP_H +#define NET_TCP_H + +#include +#include +#include +#include + +#endif //NET_TCP_H diff --git a/include/communicate/tcp_client.h b/include/communicate/tcp_client.h index f081424..5dd22d6 100644 --- a/include/communicate/tcp_client.h +++ b/include/communicate/tcp_client.h @@ -5,29 +5,28 @@ #ifndef NET_TCP_CLIENT_H #define NET_TCP_CLIENT_H -#include -#include -#include -#include +#include -#include -#include -#include -#include - -#include +#include "tcp.h" namespace Net { class TCPClient { public: + TCPClient(const std::string &ip, int port); + ~TCPClient(){ + close(fd); + } + int sendData(const std::string &data); private: + int fd{}; + struct sockaddr_in client_addr{}; std::stringstream recv_buff; diff --git a/include/communicate/tcp_server.h b/include/communicate/tcp_server.h index 90288bd..c621772 100644 --- a/include/communicate/tcp_server.h +++ b/include/communicate/tcp_server.h @@ -5,21 +5,12 @@ #ifndef NET_TCP_SERVER_H #define NET_TCP_SERVER_H -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include +// 基础依赖 +#include +// 扩展依赖 +#include "tcp.h" namespace Net { @@ -41,6 +32,7 @@ namespace Net { uint8_t readByte(); private: + int fd; int status = 0; struct sockaddr_in server_addr; diff --git a/include/communicate/tcp_session.h b/include/communicate/tcp_session.h new file mode 100644 index 0000000..24a1b78 --- /dev/null +++ b/include/communicate/tcp_session.h @@ -0,0 +1,27 @@ +// +// Created by 胡宇 on 2020/10/23. +// + +#ifndef NET_TCP_SESSION_H +#define NET_TCP_SESSION_H + +// 基础依赖 +#include + +// 扩展依赖 +#include "tcp.h" + +class TCPSession { +public: + + TCPSession(){ + + } + +private: + + +}; + + +#endif //NET_TCP_SESSION_H diff --git a/include/project.h b/include/project.h new file mode 100644 index 0000000..7773100 --- /dev/null +++ b/include/project.h @@ -0,0 +1,31 @@ +// +// Created by 胡宇 on 2020/10/23. +// + +#ifndef NET_PROJECT_H +#define NET_PROJECT_H + +// 基础 +#include +#include +#include +#include + +// 基本 +#include +#include +#include + +// 流 +#include + +// 数据结构 +#include +#include +#include + +// 高级组件 +#include +#include + +#endif //NET_PROJECT_H diff --git a/src/communicate/tcp_client.cpp b/src/communicate/tcp_client.cpp index 2f2053d..fb939fa 100644 --- a/src/communicate/tcp_client.cpp +++ b/src/communicate/tcp_client.cpp @@ -4,6 +4,8 @@ #include "communicate/tcp_client.h" +extern int errno; + void Net::TCPClient::recv_data(Net::TCPClient *client) { u_char buff[1024]; int len; @@ -13,16 +15,18 @@ void Net::TCPClient::recv_data(Net::TCPClient *client) { } void Net::TCPClient::recv_cycle() { - boost::thread recv_thread(TCPClient::recv_data, - this); + boost::thread recv_thread(TCPClient::recv_data,this); recv_thread.detach(); } void Net::TCPClient::create_socket_and_connection() { if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { - throw std::runtime_error("create socket failed."); + throw std::runtime_error(strerror(errno)); } - connect(fd, (struct sockaddr *) &client_addr, sizeof(client_addr)); + if(connect(fd, (struct sockaddr *) &client_addr, sizeof(client_addr)) < 0){ + throw std::runtime_error(strerror(errno)); + } + } int Net::TCPClient::sendData(const std::string &data) { diff --git a/src/communicate/tcp_server.cpp b/src/communicate/tcp_server.cpp index 578fe72..f70336b 100644 --- a/src/communicate/tcp_server.cpp +++ b/src/communicate/tcp_server.cpp @@ -5,6 +5,8 @@ #include "debug_tools/print_tools.h" #include "communicate/tcp_server.h" +extern int errno; + void Net::TCPServer::cycle() { boost::thread accept_manager_thread(TCPServer::accept_manager, this); this->p_accept_manager_thread = &accept_manager_thread; @@ -17,7 +19,7 @@ void Net::TCPServer::accept_manager(Net::TCPServer *server) { int connect_fd = ::accept(server->fd, nullptr, nullptr); Net::PrintTools::debugPrintSuccess("New Connection."); - if(connect_fd == -1) throw std::runtime_error("accept tcp connection error"); + if(connect_fd < 0) throw std::runtime_error(strerror(errno)); else{ boost::thread accept_thread(TCPServer::accept, connect_fd, &server->buff_mutex, &server->recv_buff, &server->status); accept_thread.detach(); @@ -45,24 +47,24 @@ void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex, std::queueserver_addr.sin_family = AF_INET; + this->server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + this->server_addr.sin_port = htons(port); } Net::TCPServer::TCPServer(int port, int max_connection) { + // 创建 socket create_socket(port); - if(bind(fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) - throw std::runtime_error("bind port failed."); + if(bind(fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) + throw std::runtime_error(strerror(errno)); - if(listen(fd, max_connection) == -1) - throw std::runtime_error("listen socket failed."); + if(listen(fd, max_connection) < 0) + throw std::runtime_error(strerror(errno)); cycle(); diff --git a/src/communicate/tcp_session.cpp b/src/communicate/tcp_session.cpp new file mode 100644 index 0000000..9652a6f --- /dev/null +++ b/src/communicate/tcp_session.cpp @@ -0,0 +1,5 @@ +// +// Created by 胡宇 on 2020/10/23. +// + +#include "communicate/tcp_session.h" diff --git a/test/test_commu/main.cpp b/test/test_commu/main.cpp index 1d83182..d88ba58 100644 --- a/test/test_commu/main.cpp +++ b/test/test_commu/main.cpp @@ -12,11 +12,12 @@ using namespace Net; int main(int argc, char *argv[]){ + PrintTools::debugPrintSuccess("All Started."); if(fork() == 0) { PrintTools::debugPrintSuccess("Child Started."); TCPClient client("127.0.0.1", 9048); for(int i = 0; i < 32; i++, usleep(1e4)) - client.sendData("Hello"); + client.sendData("Hello\n"); PrintTools::debugPrintSuccess("Child Exited."); } else{