Unix网络编程-IO多路复用Select

客户端

/**
 * @file client-select.c
 * 使用select改进client1.c中的str_cli函数
 * 这个str_cli函数是不正确的:
 * 1.标准IO缓冲区不会触发select,不建议待缓冲区的IO和select一起使用
 * 2.写完就关闭套接字,可能会有数据遗留在网络中,应该先进行半关闭
 * 
 */
// 只负责连接
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <math.h>
void hander(int p)
{
    exit(0);
}

void str_cli(FILE *fp, int client_socket)
{
    fd_set readFds;     // 用于select监听的文件描述符集合
    FD_ZERO(&readFds);  // 初始化为0
    int maxfd = fileno(fp) > client_socket ? fileno(fp) : client_socket + 1; // 最大的文件描述符 + 1
    while (1)
    {
        printf("...\n");
        FD_SET(fileno(fp), &readFds);       // 监听fp文件描述符——每次循环都必须重置
        FD_SET(client_socket, &readFds);    // 监听套接字
        select(maxfd, &readFds, NULL, NULL, NULL);      // 监听文件描述符集合里的 读 事件 —— 这一步被阻塞

        /* 解除阻塞执行下面 */

        if(FD_ISSET(client_socket, &readFds))   // 如果client_socket的读事件触发
        {
            // 开始从client_socket中读数据,显示到终端
            printf("read msg.\n");
            char buf[1024];
            int n;
            if ((n = read(client_socket, buf, 1024)) > 0)
            {
                write(STDOUT_FILENO, buf, n);
            }
            if (n == -1)
            {
                perror("read");
            }
        }
        if (FD_ISSET(fileno(fp), &readFds))     // 标准输入事件触发
        {
            printf("stdin write.\n");
            char buf[1024];
            int n;
            n = read(STDIN_FILENO, buf, 1024);
            if (n == -1)
            {
                perror("read");
                exit(-1);
            }
            write(client_socket, buf, n);
        }
    }

    return;
}

int main(int argc, char **argv)
{
    if (argc < 2)
    {
        fprintf(stderr,"Using: ./filename address port\n");
        exit(0);
    }
    signal(SIGINT, hander);

    int port = atoi(argv[2]);
    
    // 新建套接字
    int client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (client_socket < 0)
    {
        perror("socket");
        exit(-1);
    }

    // 建立服务端地址结构体
    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(port);
    inet_pton(AF_INET, argv[1], &server_address.sin_addr.s_addr);

    // 请求连接
    if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
    {
        perror("connect");
        close(client_socket);
        exit(-1);
    }
    printf("connect success, IP:%s, port:%d\n", argv[1], port);
    str_cli(stdin, client_socket);

    exit(0);
}

改进:

/**
 * @file client-select.c
 * 使用select改进client1.cpp中的str_cli函数
 * 正确版本
 * 
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <math.h>
#include <sys/select.h>
void hander(int p)
{
    exit(0);
}

void str_cli(FILE *fp, int client_socket)
{
    fd_set readfds;
    FD_ZERO(&readfds);
    int maxfd = fileno(fp) > client_socket ? fileno(fp) : client_socket + 1;
    int stdineof = 0;   // 标志 是否发生了标准输入事件
    while (1)
    {
        if (stdineof == 0)
        {
            FD_SET(fileno(fp), &readfds);
        }
        FD_SET(client_socket, &readfds);
        select(maxfd, &readfds, NULL, NULL, NULL);
        if (FD_ISSET(client_socket, &readfds))
        {
            int n;
            char buf[1024];
            if ((n = read(client_socket, buf, 1024)) == 0)
            {
                if (stdineof == 1)  // 表示输入完毕了,可以直接退出
                {
                    return;
                }
            }
            write(fileno(fp), buf, n);
        }
        if (FD_ISSET(fileno(fp), &readfds))
        {
            int n;
            char buf[1024];
            n = read(fileno(fp), buf, 1024);
            if (n == 0)     // 没有输入,则表示输入完毕
            {
                stdineof = 1;
                shutdown(client_socket, SHUT_WR);   // 关闭套接字的写端
                FD_CLR(fileno(fp), &readfds);   // 不再监听fp
            }
            write(client_socket, buf, n);
        }
    }

    return;
}

int main(int argc, char **argv)
{
    if (argc < 2)
    {
        fprintf(stderr,"Using: ./filename address port\n");
        exit(0);
    }
    signal(SIGINT, hander);

    int port = atoi(argv[2]);
    
    // 新建套接字
    int client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (client_socket < 0)
    {
        perror("socket");
        exit(-1);
    }

    // 建立服务端地址结构体
    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(port);
    inet_pton(AF_INET, argv[1], &server_address.sin_addr.s_addr);

    // 请求连接
    if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
    {
        perror("connect");
        close(client_socket);
        exit(-1);
    }
    printf("connect success, IP:%s, port:%d\n", argv[1], port);
    str_cli(stdin, client_socket);

    exit(0);
}

服务端

/**
 * @file server-select.c
 * 单进程,使用select实现多个已经连接套接字的通信
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <errno.h>
#define PORT 1227
#define ADDRESS "127.0.0.1"


int main(int argc, char **argv)
{
    int listenSocket;

    // socket
    listenSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (listenSocket < 0)
    {
        perror("listen");
        exit(-1);
    }

    struct sockaddr_in serveraddr;
    bzero(&serveraddr, 0);  // 初始化,好习惯
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(PORT);
    inet_pton(AF_INET, ADDRESS, &serveraddr.sin_addr.s_addr);
    
    // bind
    if (bind(listenSocket, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
    {
        perror("bind");
        exit(-1);
    }

    // listen
    listen(listenSocket, 200);

    int clients[FD_SETSIZE], clientsSize;
    int maxfd = listenSocket;
    
    // 数组初始化
    for (int i = 0; i < FD_SETSIZE; ++i)
    {
        // 初始化-1
        clients[i] = -1;
    }

    fd_set allset;
    FD_ZERO(&allset);
    FD_SET(listenSocket, &allset);

    // 循环事物
    while (1)
    {
        fd_set curset = allset;
        int maxi;
        int readyNum = select(maxfd + 1, &curset, NULL, NULL, NULL);

        // 如果是listensocket可读,说明有连接
        if (FD_ISSET(listenSocket, &curset))
        {
            printf("have new connect.\n");
            int clientSocket;
            struct sockaddr_in clientaddr;
            bzero(&clientaddr, 0);
            socklen_t clientaddrlen = sizeof(clientaddr);

            clientSocket = accept(listenSocket, (struct sockaddr *)&clientaddr, &clientaddrlen);
            if (clientSocket < 0)
            {
                perror("accept");
                exit(-1);
            }

            // 把这个通信套接字保存在第一个空位数组里
            int i;
            for (i = 0; i < FD_SETSIZE; ++i)
            {
                if (clients[i] == -1)
                {
                    clients[i] = clientSocket;
                    break;
                }
            }
            if (i == FD_SETSIZE)    // 没有空位置了
            {
                fprintf(stderr, "too many clients.\n");
                close(clientSocket);
                // continue;  不能continue,因为可能还有别的通信套接字要处理
            }
            if (i > maxi)
            {
                maxi = i;   // 数组的最大位置,用来缩减遍历
            }

            // 把这个客户端套接字加入到文件描述符集合里
            FD_SET(clientSocket, &allset);
            if (clientSocket > maxfd)   // 更新最大文件描述符
            {
                maxfd = clientSocket;
            }

            --readyNum;
            if (readyNum == 0)
            {
                continue;
            }
        }

        // readyNum != 0, 继续处理活动的通信套接字
        int sockfd;
        char buf[1024];
        for (int i = 0; i <= maxi; ++i)
        {
            if (clients[i] < 0)
            {
                continue;
            }
            sockfd = clients[i];
            if (FD_ISSET(sockfd, &curset))  // 如果这个套接字有活动
            {
                int n = read(sockfd, buf, 1024);
                if (n < 0)
                {
                    perror("read");
                    exit(-1);
                }
                else if (n == 0)    // 注意,这里0只能表示文件结束,对于套接字,就是对端关闭了套接字,发送EOF
                {
                    close(sockfd);
                    printf("have socket close.\n");
                    clients[i] = -1;
                    FD_CLR(sockfd, &allset);
                }
                else
                {
                    printf("[%d]:%s.\n", sockfd, buf);
                    write(sockfd, buf, n);
                }
                --readyNum; // 所有的活动套接字都处理完了
                if (readyNum == 0)
                {
                    break;
                }
            }
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙子砰砰枪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值