前言
由于TCP是面向连接的,所以在创建套接字之后还需要进入监听状态,监听状态下可以获取客户端的请求。获得请求之后,服务器需要接受连接,之后再处理事务。
实现服务端具体步骤
总的来说,TCP服务端主要实现以下步骤:
- 创建一个监听套接字对象,指定使用TCP协议
- 绑定套接字到特定的地址和端口号
- 调用listen方法,监听连接请求
- 接收连接,返回一个新的套接字用于与客户端通信,以及客户端的地址
- 处理请求
- 发送响应
- 本次连接结束,关闭套接字
- 服务器结束,关闭监听套接字
为了便于区分,我们将第一个创建的套接字称为监听套接字,监听套接字只获取连接。后创建的套接字用于处理业务。
实现客户端具体步骤
- 创建套接字
- 使用connect方法连接到服务器
- 发送请求
- 接收响应
- 关闭套接字,结束连接
实现服务端
TcpServer.hpp
该头文件定义了一个服务器类,用于实现上述服务器的各个功能,具体代码如下:
#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>
#include <fstream>
#include <functional>
#include "InetAddr.hpp"
#include "Log.hpp"using namespace log_ns;const static int gport = 8888;
const static int gsockfd = -1;
const static int gblcklog = 8;using task_t = function<void()>;class TcpServer
{
private:
public:TcpServer(uint16_t port = gport): _port(port), _listensockfd(gsockfd), _isrunning(false){}void InitServer(){// 1.创建socket,选择TCP协议_listensockfd = socket(AF_INET, SOCK_STREAM, 0);if (_listensockfd < 0){LOG(FATAL, "socket create error\n");exit(1);}LOG(FATAL, "listen socket create success,sockfd is %d\n", _listensockfd);// 创建strcut sockaddr_instruct 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;// 2. bindint res = bind(_listensockfd, (struct sockaddr *)&local, sizeof(local));if (res < 0){LOG(FATAL, "server listen bind failed\n");exit(1);}LOG(FATAL, "server listen bind success\n");// 3.设置监听状态if (listen(_listensockfd, gblcklog) < 0){LOG(FATAL, "listen error\n");exit(1);}LOG(INFO, "listen suceess\n");}void Loop(){_isrunning = true;while (_isrunning){struct sockaddr_in client;socklen_t len = sizeof(client);// 4.获取新连接int sockfd = accept(_listensockfd, (struct sockaddr *)&client, &len);if (sockfd < 0){LOG(WARNING, "accept error\n");continue;}InetAddr addr(client);LOG(INFO, "get a new link,client info:%s,sockfd is %d\n", addr.AddrStr().c_str(), sockfd);// 处理业务// v0--简单读取消息Service(sockfd, addr);}_isrunning = false;}void Service(int sockfd, InetAddr addr){while (true){// 接收消息char inbuff[1024];ssize_t n = read(sockfd, inbuff, sizeof(inbuff) - 1);if (n > 0){inbuff[n] = 0;LOG(INFO, "get message from client ,message is %s\n", inbuff);// 回响string echo_message = "[server say]# ";echo_message += inbuff;write(sockfd, echo_message.c_str(), echo_message.size());}else if (n == 0){LOG(INFO, "client %s quit\n", addr.AddrStr().c_str());break;}else{LOG(ERROR, "read error: %s\n", addr.AddrStr().c_str());break;}}close(sockfd);}private:uint16_t _port;int _listensockfd;bool _isrunning;
};
TcpServerMain.cpp
该文件定义了服务端对象,执行服务端逻辑
#include "TcpServer.hpp"
#include <iostream>
#include <memory>
int main(int argc, char *argv[])
{if (argc != 2){std::cerr << "Usage: " << argv[0] << " local-port" << std::endl;exit(0);}uint16_t port = std::stoi(argv[1]);std::unique_ptr<TcpServer> tcvr = make_unique<TcpServer>(port);tcvr->InitServer();tcvr->Loop();return 0;
}
实现客户端
TcpClientMain.cpp
该文件实现了客户端发送请求的逻辑
#include <iostream>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>int main(int argc, char *argv[])
{if (argc != 3){std::cerr << "Usage : " << argv[0] << " server-ip server-port\n"<< std::endl;exit(0);}std::string ip = argv[1];uint16_t port = std::stoi(argv[2]);// 1.创建sockfdint sockfd = socket(AF_INET, SOCK_STREAM, 0);if (sockfd < 0){std::cerr << "create socket error" << std::endl;exit(0);}struct sockaddr_in peer;memset(&peer, 0, sizeof(peer));peer.sin_family = AF_INET;inet_pton(AF_INET, ip.c_str(), &peer.sin_addr);peer.sin_port = htons(port);// 2.连接服务器int n = connect(sockfd, (struct sockaddr *)&peer, sizeof(peer));if (n < 0){std::cerr << "connect socket error" << std::endl;exit(2);}// 3.处理业务while (true){std::string message;std::cout << "Enter #";std::getline(std::cin, message);write(sockfd, message.c_str(), message.size());char echo_buff[1024];int res = read(sockfd, echo_buff, sizeof(echo_buff) - 1);if (res > 0){echo_buff[res] = 0;std::cout << echo_buff << std::endl;}else{break;}}close(sockfd);return 0;
}