Modified.
This commit is contained in:
parent
834500c0c4
commit
2d15bc935e
@ -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)
|
||||
|
||||
|
13
include/communicate/tcp.h
Normal file
13
include/communicate/tcp.h
Normal file
@ -0,0 +1,13 @@
|
||||
//
|
||||
// Created by 胡宇 on 2020/10/23.
|
||||
//
|
||||
|
||||
#ifndef NET_TCP_H
|
||||
#define NET_TCP_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#endif //NET_TCP_H
|
@ -5,29 +5,28 @@
|
||||
#ifndef NET_TCP_CLIENT_H
|
||||
#define NET_TCP_CLIENT_H
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <project.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <boost/thread/thread.hpp>
|
||||
#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;
|
||||
|
@ -5,21 +5,12 @@
|
||||
#ifndef NET_TCP_SERVER_H
|
||||
#define NET_TCP_SERVER_H
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
// 基础依赖
|
||||
#include <project.h>
|
||||
|
||||
// 扩展依赖
|
||||
#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;
|
||||
|
27
include/communicate/tcp_session.h
Normal file
27
include/communicate/tcp_session.h
Normal file
@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by 胡宇 on 2020/10/23.
|
||||
//
|
||||
|
||||
#ifndef NET_TCP_SESSION_H
|
||||
#define NET_TCP_SESSION_H
|
||||
|
||||
// 基础依赖
|
||||
#include <project.h>
|
||||
|
||||
// 扩展依赖
|
||||
#include "tcp.h"
|
||||
|
||||
class TCPSession {
|
||||
public:
|
||||
|
||||
TCPSession(){
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //NET_TCP_SESSION_H
|
31
include/project.h
Normal file
31
include/project.h
Normal file
@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by 胡宇 on 2020/10/23.
|
||||
//
|
||||
|
||||
#ifndef NET_PROJECT_H
|
||||
#define NET_PROJECT_H
|
||||
|
||||
// 基础
|
||||
#include <cerrno>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
|
||||
// 基本
|
||||
#include <stdexcept>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
// 流
|
||||
#include <sstream>
|
||||
|
||||
// 数据结构
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
|
||||
// 高级组件
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
|
||||
#endif //NET_PROJECT_H
|
@ -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) {
|
||||
|
@ -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::queue<uint8_t
|
||||
}
|
||||
|
||||
void Net::TCPServer::create_socket(int port) {
|
||||
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if(!~fd) throw std::runtime_error("could not create socket file.");
|
||||
if((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) throw std::runtime_error(strerror(errno));
|
||||
|
||||
std::memset(&server_addr, 0, sizeof(struct sockaddr_in));
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
server_addr.sin_port = htons(port);
|
||||
this->server_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();
|
||||
|
5
src/communicate/tcp_session.cpp
Normal file
5
src/communicate/tcp_session.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by 胡宇 on 2020/10/23.
|
||||
//
|
||||
|
||||
#include "communicate/tcp_session.h"
|
@ -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{
|
||||
|
Loading…
Reference in New Issue
Block a user