4.22、多线程实现并发服务器

4.22、多线程实现并发服务器

  • 1.服务器代码
  • 2.客户端代码

1.服务器代码

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;


struct SockInfo {
    sockaddr_in addr;
    pthread_t tid;
    int fd;
};

// 定义线程的最大数量
const int INFO_SIZE = 128;

SockInfo sockInfos[INFO_SIZE];

void * working(void * arg) {
    // 需要对接受到的客户端进行操作,需要传入以下参数
    // 线程的 tid,客户端的信息 client_addr ,文件描述符 
    
    SockInfo * sockInfo = (SockInfo *) arg;
    pthread_t client_tid = sockInfo->tid;
    sockaddr_in client_addr = sockInfo->addr;
    int client_fd = sockInfo->fd;

    // 输出客户端的信息
    char client_ip[16];
    // 获取客户端ip
    inet_ntop(AF_INET, &sockInfo->addr.sin_addr.s_addr, client_ip, sizeof(client_ip));
    uint16_t client_port = ntohs(sockInfo->addr.sin_port);
    printf("client ip: %s, client port: %hu\n", client_ip, client_port);

    char recvBuf[1024];
    while (1) {

        // 读取客户端数据
        int read_ret = read(client_fd, recvBuf, sizeof(recvBuf));
        if (read_ret == -1) {
            perror("read");
            exit(-1);
        } else if (read_ret == 0) {
            cout << "client read closed ... " << endl;
            break;
        } else if (read_ret > 0) {
            printf("I am server, client tid: %lu, data: %s\n", client_tid, recvBuf);
        }

        // 向客户端返回数据
        int wtitr_ret = write(client_fd, recvBuf, strlen(recvBuf) + 1);
        if (wtitr_ret == -1) {
            perror("write");
            pthread_exit((void *)"write error");
        } else if (wtitr_ret == 0) {
            cout << "client write closed ... " << endl;
            break;
        }

        
    }
    close(client_fd);

    pthread_exit(NULL);
}

int main() {


    // 创建监服务器的监听文件描述符
    int server_listen_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_listen_fd == -1) {
        perror("socket");
        exit(-1);
    }


    // 绑定服务器的ip和端口
    sockaddr_in server_addr;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(9999);

    int bind_ret = bind(server_listen_fd, (sockaddr *)&server_addr, sizeof(server_addr));
    if (bind_ret == -1) {
        perror("bind");
        exit(-1);
    }



    // 设置监听数量
    int listen_ret = listen(server_listen_fd, 8);
    if (listen_ret == -1) {
        perror("listen");
        exit(-1);
    }

    // 初始化线程的数据
    for (int i = 0; i < INFO_SIZE; i ++ ) {
        sockInfos[i].fd = -1;
        sockInfos[i].tid = -1;
    }

    while (1) {

        // 等待客户端进行连接
        sockaddr_in client_addr;
        socklen_t client_addr_len = sizeof(client_addr);

        int client_fd = accept(server_listen_fd, (sockaddr *)&client_addr, &client_addr_len);
        if (client_fd == -1) {
            perror("accept");
            exit(-1);
        }

        // 接受完之后创建线程进行通信
        SockInfo * clientSockInfo;

        // 判断是否还有空余的线程进行通信
        for (int i = 0; i < INFO_SIZE; i ++ ) {
            if (sockInfos[i].fd == -1 && sockInfos[i].tid == -1) {
                clientSockInfo = &sockInfos[i];
                break;
            }

            if (i == INFO_SIZE - 1) {
                cout << "服务端支持的连接已满,请等待一下" << endl;
                sleep(1);
                i = -1;
            }
        }

        // 将参数赋值
        clientSockInfo->fd = client_fd;
        clientSockInfo->addr = client_addr;
        
        // 创建线程
        int pcreat_ret = pthread_create(&clientSockInfo->tid, NULL, working, clientSockInfo);
        if (pcreat_ret) {
            perror("pthread_create");
            exit(-1);
        }

        // 设置线程分离
        pthread_detach(clientSockInfo->tid);

    }

    close(server_listen_fd);

    pthread_exit(NULL);


    return 0;
}

2.客户端代码

#include 
#include 
#include 
#include 

using namespace std;

int main() {

    // 创建客户端通信的 fd
    int client_fd = socket(PF_INET, SOCK_STREAM, 0);
    if (client_fd == -1) {
        perror("socket");
        exit(-1);
    }

    // 向服务端发起连接
    sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    // 转换端口
    server_addr.sin_port = htons(9999);
    char server_ip[] = "xxx.xxx.xxx.xxx";	//自己的ip
    // 转换 ip 地址
    inet_pton(AF_INET, server_ip, &server_addr.sin_addr.s_addr);
    int connect_ret = connect(client_fd, (sockaddr *)&server_addr, sizeof(server_addr));
    if (connect_ret == -1) {
        perror("connect");
        exit(-1);
    }

    char recvBuf[1024];
    int i = 0;
    while (1) {

        // 向服务端写入数据
        sprintf(recvBuf, "client data i: %d\n",  ++ i);
        int write_ret = write(client_fd, recvBuf, strlen(recvBuf) + 1);
        if (write_ret == -1) {
            perror("write");
            exit(-1);
        } else if (write_ret == 0) {
            cout << "client closed write" << endl;
            break;
        }

        // 读取数据
        int read_ret = read(client_fd, recvBuf, sizeof(recvBuf));
        if (read_ret > 0) {
            printf("I am client pid: %d, server data: %s\n", getpid(), recvBuf);
        } else if (read_ret == 0) {
            cout << "server closed..." << endl;
            break;
        } else if (read_ret == -1) {
            perror("read");
            exit(-1);
        }

        sleep(1);
    }

    return 0;

}

你可能感兴趣的:(服务器,c++,运维)