【无标题】

//
// Created by 七七 on 2023/4/26.
//
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#ifndef SOCKETDEMO_CTCPSERVER_H
#define SOCKETDEMO_CTCPSERVER_H


//定义TCP服务端类
class CTcpServer{
public:
    int m_listenfd;
    std::list<int> m_clientfds;
    //构造函数
    CTcpServer();

    //初始化服务端
    bool initServer();

    //accept
    int Accept();

    //send data
    int Send(int fd,void *buff, int bufflen);

    //recv data
    int Recv(int fd,void *buff, int bufflen);

    //close listen fd
    void listenClose();

    //close client fd
    void clientClose(int fd);

    //析构函数
    ~CTcpServer();
};


#endif //SOCKETDEMO_CTCPSERVER_H

//
// Created by 七七 on 2023/4/26.
//

#include "CTcpServer.h"
//构造函数
CTcpServer::CTcpServer(){
    m_listenfd = 0;
}

//析构函数
CTcpServer::~CTcpServer(){
    if(m_listenfd!=0) close(m_listenfd);
    auto it = m_clientfds.begin();
    while (it != m_clientfds.end()) {
        close(*it);
        ++it;
    }
}

//初始化
bool CTcpServer::initServer(){

    if (m_listenfd!=0) { close(m_listenfd); m_listenfd=0; }
    m_listenfd = socket(AF_INET,SOCK_STREAM,0);     // 创建服务端的socket

    // 把服务端用于通信的地址和端口绑定到socket上
    struct sockaddr_in servaddr;                    // 服务端地址信息的数据结构
    memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family = AF_INET;                  // 协议族,在socket编程中只能是AF_INET

    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);   // 本主机的任意ip地址
    servaddr.sin_port = htons(8000);                // 绑定通信端口

    if (bind(m_listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) != 0 )
    {close(m_listenfd); m_listenfd=0; return false;}

    // 把socket设置为监听模式
    if (listen(m_listenfd,5) != 0 ) { close(m_listenfd); m_listenfd=0; return false; }

    return true;
}

//accept
int CTcpServer::Accept(){
    int clientfd = accept(m_listenfd, NULL, 0);
    if(clientfd == -1) return clientfd;
    m_clientfds.push_back(clientfd);
    return clientfd;
}

//send
int CTcpServer::Send(int fd, void *buff, int bufflen){
    return send(fd, buff, bufflen, 0);
}

//recv
int CTcpServer::Recv(int fd, void *buff, int bufflen){
    return recv(fd, buff, bufflen, 0);
}

//listenclose
void CTcpServer::listenClose(){
    if (m_listenfd!=0) { close(m_listenfd); m_listenfd=0;}
}

//clientclose
void CTcpServer::clientClose(int fd){
    if (fd!=0) { close(fd);}
}
#include 
#include "CTcpServer.h"
//main函数实现
int main()
{


    //实例化一个server类
    CTcpServer tcpserver;

    //初始化服务器
    if(tcpserver.initServer() == false) {printf("init server error\n"); return false;}

    while(1){
        int fd = tcpserver.Accept();
        if(fd == -1) continue;
        printf("##############server start###############\n");

        std::thread t([&tcpserver, fd]() {
            char buff[1024+1] = {0};
            while(tcpserver.Recv(fd,buff, sizeof(buff)) > 0){
                printf("server recv: %s\n", buff);
                strcat(buff, "---@ok");
                tcpserver.Send(fd,buff, strlen(buff));
                memset(buff, 0x00, sizeof(buff));
                printf("----------------------------------------\n");
            }
        });
        t.detach();
        //return 0;
    }
    return 0;
}

你可能感兴趣的:(网络)