一、服务端
1.tcpServer.hpp
此文件负责实现一个tcp服务器
#pragma once
#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>// 错误码
enum
{USAGE_ERR = 1,SOCKET_ERR,BIND_ERR,LISTEN_ERR
};static const uint16_t gport = 8080; // 默认端口号
static const int gbacklog = 5; // listen第二个参数// TCP服务器
class TcpServer
{
public:// 构造函数TcpServer(const uint16_t &port = gport): _listensock(-1), _port(port){}// 析构函数~TcpServer() {}// 初始化服务器void initServer()// 启动服务器void start()// 处理IOvoid serviceIO(int sock)private:int _listensock; // 不是用来进行数据通信的,他是用来监听链接到来,获取新链接的!uint16_t _port; // 端口号
};
(1)initServer()
void initServer()
{// 1.创建socket文件套接字对象_listensock = socket(AF_INET, SOCK_STREAM, 0);if (_listensock < 0){logMessage(FATAL, "create socket error");exit(SOCKET_ERR);}logMessage(NORMAL, "create socket success:%d", _listensock);// 2.bind绑定自己的网络信息struct sockaddr_in local;memset(&local, 0, sizeof(local));local.sin_family = AF_INET;local.sin_port = htons(_port);local.sin_addr.s_addr = INADDR_ANY;if (bind(_listensock, (struct sockaddr *)&local, sizeof(local)) < 0){logMessage(FATAL, "bind socket error");exit(BIND_ERR);}logMessage(NORMAL, "bind socket success");// 3.设置socket 为监听状态if (listen(_listensock, gbacklog) < 0){logMessage(FATAL, "listen socket error");exit(LISTEN_ERR);}logMessage(NORMAL, "listen socket success");
}
(2)start()
void start()
{for (;;){// 4.server 获取新链接// sock才是和client进行通信的fdstruct sockaddr_in peer;socklen_t len = sizeof(peer);int sock = accept(_listensock, (struct sockaddr *)&peer, &len);if (sock < 0){logMessage(ERROR, "accept error, next");continue;}logMessage(NORMAL,"accept a new link success, get new sock:%d",sock);// 5.未来通信我们就用这个sock,面向字节流的,后续全部都是文件操作!serviceIO(sock);close(sock); // 要关闭使用完的sock,要不然会导致文件描述符泄露}
}
(3)serviceIO()
void serviceIO(int sock)
{char buffer[1024]; // 读到的数据放这里while (true){// 读数据(接收数据)ssize_t n = read(sock, buffer, sizeof(buffer) - 1);if (n > 0){buffer[n] = 0;std::cout << "receive message: " << buffer << std::endl;std::string outbuffer = buffer;// 写数据(发送数据 -> 客户端)write(sock, outbuffer.c_str(), outbuffer.size());}else if (n == 0){// 代表client退出logMessage(NORMAL, "client quit");break;}}
}
2.tcpServer.cpp
此文件负责tcp服务器的调用逻辑
#include "tcpServer.hpp"
#include <memory>using namespace std;// 使用手册
static void Usage(string proc)
{cout << "\nUsage:\n\t" << proc << " local_port\n\n";
}// ./tcpServer local_port
int main(int argc, char *argv[])
{if(argc != 2){Usage(argv[0]);exit(USAGE_ERR);}uint16_t port = atoi(argv[1]);unique_ptr<TcpServer> tsvr(new TcpServer(port));tsvr->initServer();tsvr->start();return 0;
}
二、客户端
1.tcpClient.hpp
此文件负责实现一个tcp客户端
#pragma once#include <iostream>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>#define NUM 1024class TcpClient
{
public:// 构造函数TcpClient(const std::string &serverip, const uint16_t &serverport):_sock(-1), _serverip(serverip), _serverport(serverport){}// 析构函数~TcpClient(){if(_sock >= 0)close(_sock);}// 初始化客户端void initClient()// 启动客户端void start()private:int _sock;std::string _serverip;uint16_t _serverport;
};
(1)initClient()
void initClient()
{// 1.创建socket_sock = socket(AF_INET, SOCK_STREAM, 0);if (_sock < 0){std::cerr << "socket create error" << std::endl;exit(2);}// 2.客户端不需要自己bind,让OS来bind// 3.要不要listen?不要!// 4.要不要accept?不要!
}
(2)start()
void start()
{// 5.要发起连接!struct sockaddr_in server;memset(&server, 0, sizeof(server));server.sin_family = AF_INET;server.sin_port = htons(_serverport);server.sin_addr.s_addr = inet_addr(_serverip.c_str());if (connect(_sock, (struct sockaddr *)&server, sizeof(server)) != 0){std::cerr << "socket connet error" << std::endl;}else{// 成功连接std::string msg;while (true){std::cout << "Enter# ";std::getline(std::cin, msg);write(_sock, msg.c_str(), msg.size());char buffer[NUM];int n = read(_sock, buffer, sizeof(buffer) - 1);if (n > 0){// 目前我们可以吧读到的数据当成字符串buffer[n] = 0;std::cout << "Server回显# " << buffer << std::endl;}else{break;}}}
}
2.tcpClient.cpp
此文件负责tcp客户端的调用逻辑
#include "tcpClient.hpp"
#include <memory>// 使用手册
static void Usage(std::string proc)
{std::cout << "\nUsage:\n\t" << proc << " serverip serverport\n\n";
}// ./tcpclient serverip serverport
int main(int argc, char *argv[])
{if(argc != 3){Usage(argv[0]);exit(1);}std::string serverip = argv[1];uint16_t serverport = atoi(argv[2]);std::unique_ptr<TcpClient> tcli(new TcpClient(serverip, serverport));tcli->initClient();tcli->start();return 0;
}
三、整体调用逻辑
1.双方调用逻辑
上面写的TCP服务器还不支持多个客户同时访问,分析如图
引入多线程实现多个客户同时访问
2.调整对应代码
(1)新增线程数据
class TcpServer;// 线程数据
class ThreadData
{
public:ThreadData(TcpServer *self, int sock): _self(self), _sock(sock){}public:TcpServer *_self;int _sock;
};
(2)线程要执行的函数
// 多线程版 需要的函数
static void *threadRoutine(void *args)
{// 线程分离 -> 主进程不用在等待pthread_detach(pthread_self());// 通过传参拿到线程数据ThreadData *td = static_cast<ThreadData *>(args);// 进行IOtd->_self->serviceIO(td->_sock);close(td->_sock);delete td;return nullptr;
}
(3)调整服务器start()函数
void start()
{for (;;){// 4.server 获取新链接struct sockaddr_in peer;socklen_t len = sizeof(peer);int sock = accept(_listensock, (struct sockaddr *)&peer, &len);if (sock < 0){logMessage(ERROR, "accept error, next");continue;}logMessage(NORMAL, "accept a new link success, get new sock:%d", sock);// 5.未来通信我们就用这个sockpthread_t tid;ThreadData *td = new ThreadData(this, sock);pthread_create(&tid, nullptr, threadRoutine, (void *)td);}
}
3.展示运行结果