UNIX网络编程:socket & select() 实现clients/server通信

一、问题引入

UNIX网络编程 卷1:套接字联网API(第三版) 第6章 介绍了I/O复用可以通过select()的单进程服务器与多客户端通信。

UNIX下可用的5中I/O模型:

  • 阻塞式I/O
  • 非阻塞式I/O
  • I/O复用(select和poll)
  • 信号驱动式I/O(SIGIO)
  • 异步I/O(POSIX的aio_系列函数)

其中前面4种可以分为同步I/O,第五种为异步I/O。

二、解决过程

2-1 client 代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>

#define IP "10.8.198.227"
#define PORT 8887
#define BUF_MAX_SIZE 1024

#define	min(a,b)	((a) < (b) ? (a) : (b))
#define	max(a,b)	((a) > (b) ? (a) : (b))

static int handle(int connect_fd, const char *socket)
{
    char buf[BUF_MAX_SIZE], read_buf[BUF_MAX_SIZE];
    int n;
    int maxfdp1, stdineof = 0;
    fd_set rset;

    FD_ZERO(&rset);
    while (1)
    {
        if (stdineof == 0)
            FD_SET(fileno(stdin), &rset);
        FD_SET(connect_fd, &rset);
        maxfdp1 = max(fileno(stdin), connect_fd) + 1;
        if (select(maxfdp1, &rset, NULL, NULL, NULL) < 0)
        {
            perror("select error");
            exit(1);
        }

        memset(buf, 0, BUF_MAX_SIZE);
        if (FD_ISSET(connect_fd, &rset)) // socket is readable
        {
            n = read(connect_fd, buf, BUF_MAX_SIZE);
            if (n == -1)
            {
                perror("read error");
                exit(1);
            }
            else if (n == 0)
            {
                if (stdineof == 1)
                    return 1;
                else
                {
                    perror("server terminated prematurely");
                    exit(1);
                }
            }
            memset(read_buf, 0, BUF_MAX_SIZE);
            sprintf(read_buf, "%s:%s(%d Byte)\n", socket, buf, n);
            write(fileno(stdout), read_buf, strlen(read_buf));
        }
        if (FD_ISSET(fileno(stdin), &rset)) // input is readable
        {
            n = read(fileno(stdin), buf, BUF_MAX_SIZE);
            if (n == -1)
            {
                perror("read error");
                exit(1);
            }
            else if (n == 0)
            {
                stdineof = 1;
                shutdown(connect_fd, SHUT_WR);
                FD_CLR(fileno(stdin), &rset);
                continue;
            }
            if (buf[n-1] == '\n') // remove the character '\n'
            {
                buf[n-1] = '\n';
                n = n - 1;
            }
            write(connect_fd, buf, n);
        }
    }
    return 0;
}

int main(void)
{
    int sockfd;
    char buf[1024];
    struct sockaddr_in server_addr;
    char server_socket[128];

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = inet_addr(IP);
    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        printf("connect error \n");
        return -1;
    }
    printf("server addr:%s por:%d\n",
           inet_ntop(AF_INET, &server_addr.sin_addr, buf, sizeof(buf)),
             ntohs(server_addr.sin_port));
    snprintf(server_socket, sizeof(server_socket), "server socket (%s:%d)",
             inet_ntop(AF_INET, &server_addr.sin_addr, buf, sizeof(buf)),
             ntohs(server_addr.sin_port));
    handle(sockfd, server_socket);
    close(sockfd);

    return EXIT_SUCCESS;
}

2-2 server 代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MYPORT 8887
#define BACKLOG 5
#define BUF_SIZE 1024

typedef struct CLIENT_CONN_ST
{
    int connfd;
    char socket[128];
    struct sockaddr_in cliaddr;
} CLIENT_CONN_ST;

static CLIENT_CONN_ST g_client_conn[BACKLOG];

static void client_connect_index_init(void)
{
    for (int i = 0; i < BACKLOG; i++)
    {
        memset(&g_client_conn[i], 0, sizeof(struct CLIENT_CONN_ST));
        g_client_conn[i].connfd = -1;
    }
}

static int client_connect_index_find(void)
{
    int i;
    for (i = 0; i < BACKLOG; i++)
    {
        if (g_client_conn[i].connfd == -1)
            break;
    }
    return i;
}

static int client_connect_close(void)
{
    for (int i = 0; i < BACKLOG; i++)
    {
        if (g_client_conn[i].connfd != -1)
        {
            close(g_client_conn[i].connfd);
            g_client_conn[i].connfd = -1;
        }
    }
}

static int string_toupper(const char *src, int str_len, char *dst)
{
    int i;
    for (i = 0; i < str_len; i++)
    {
        dst[i] = toupper(src[i]);
    }
    return i;
}

int main(void)
{
    int listenfd, connfd;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    socklen_t addr_len;
    int opt = 1;
    char buf[BUF_SIZE];
    int recv_len, send_len;
    char read_buf[BUF_SIZE], write_buf[BUF_SIZE];
    int ret;
    int idx;

    fd_set fdsr;
    int maxsock;
    struct timeval tv;

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(MYPORT);
    server_addr.sin_addr.s_addr = INADDR_ANY;
    if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        exit(EXIT_FAILURE);
    }
    // 端口复用
    if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
    {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }
    if (bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("bind");
        exit(EXIT_FAILURE);
    }
    if (listen(listenfd, BACKLOG) < 0)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    printf("listen port %d\n", MYPORT);
    maxsock = listenfd;
    client_connect_index_init();
    while (1)
    {
        // initialize file descriptor set
        FD_ZERO(&fdsr);
        FD_SET(listenfd, &fdsr);
        // initialize timeout val
        tv.tv_sec = 30;
        tv.tv_usec = 0;
        // add active connection to fd set
        for (int i = 0; i < BACKLOG; i++)
        {
            if (g_client_conn[i].connfd != -1)
                FD_SET(g_client_conn[i].connfd, &fdsr);
        }
        ret = select(maxsock + 1, &fdsr, NULL, NULL, &tv);
        if (ret < 0)
        {
            perror("select");
            break;
        }
        else if (ret == 0)
        {
            printf("30s timeout\n");
            continue;
        }
        // check every fd in the set
        for (int i = 0; i < BACKLOG; i++)
        {
            if (FD_ISSET(g_client_conn[i].connfd, &fdsr))
            {
                memset(read_buf, 0, sizeof(read_buf));
                memset(write_buf, 0, sizeof(write_buf));
                recv_len = read(g_client_conn[i].connfd, read_buf, sizeof(read_buf));
                if (recv_len <= 0) // client close
                {
                    printf("%s close\n", g_client_conn[i].socket);
                    close(g_client_conn[i].connfd);
                    FD_CLR(g_client_conn[i].connfd, &fdsr);
                    g_client_conn[i].connfd = -1;
                }
                else
                {
                    printf("%s:%s(%d Byte)\n", g_client_conn[i].socket, read_buf, recv_len);
                    if (strcmp("exit", read_buf) == 0)
                    {
                        printf("%s exit\n", g_client_conn[i].socket);
                        close(g_client_conn[i].connfd);
                        FD_CLR(g_client_conn[i].connfd, &fdsr);
                        g_client_conn[i].connfd = -1;
                    }
                    else
                    {
                        send_len = string_toupper(read_buf, strlen(read_buf), write_buf);
                        write(g_client_conn[i].connfd, write_buf, send_len);
                    }
                }
            }
        }
        // check whether a new connection comes
        if (FD_ISSET(listenfd, &fdsr))
        {
            memset(&client_addr, 0, sizeof(client_addr));
            addr_len = sizeof(client_addr);
            connfd = accept(listenfd, (struct sockaddr *)&client_addr, &addr_len);
            if (connfd <= 0)
            {
                perror("accept");
                continue;
            }
            idx = client_connect_index_find();
            if (idx == BACKLOG)
            {
                printf("client connected upper limit, refused connect\n");
                close(connfd);
                continue;
            }
            // add to fd queue
            g_client_conn[idx].connfd = connfd;
            memset(&g_client_conn[idx].socket, 0, sizeof(g_client_conn[idx].socket));
            printf("client addr:%s por:%d\n",
                   inet_ntop(AF_INET, &client_addr.sin_addr, buf, sizeof(buf)),
                   ntohs(client_addr.sin_port));
            snprintf(g_client_conn[idx].socket, sizeof(g_client_conn[idx].socket), "client socket (%s:%d)",
                     inet_ntop(AF_INET, &client_addr.sin_addr, buf, sizeof(buf)),
                     ntohs(client_addr.sin_port));
            if (connfd > maxsock)
                maxsock = connfd;
        }
    }
    // close all connections
    client_connect_close();

    exit(EXIT_SUCCESS);
}

2-3 编译运行结果

  • 编译client.c、server.c
gcc client.c -g -std=gnu99 -o client
gcc server.c -g -std=gnu99 -o server
  • 客户端连接服务器

  • 客户端与服务器通信

  • 客户端1主动断开连接

  • 服务器进程结束

三、反思总结

3-1 服务器解读

通过select()的I/O复用可以实现多客户端通信。

ret = select(maxsock + 1, &fdsr, NULL, NULL, &tv); 完成了对新客户端连接和已连接客户端的监控,可以在循环中检查判断I/O的状态。

由于是多客户端程序,故需要一个for语句来判断每一个已连接套接字的I/O状态。

for (int i = 0; i < BACKLOG; i++)
{
    if (FD_ISSET(g_client_conn[i].connfd, &fdsr))
    {
        ;
    }
  • 注意事项1:在每次调用select()之前,都要对timeval结构进行初始化

  • 注意事项2:在每次调用FD_SET()之前,都要对fd_set结构进行初始化,初始化函数FD_ZERO()

  • 注意事项3:select()函数中的第一次参数为指定待测试的描述符的个数,它的值是待测试的最大描述符加1

3-2 客户端解读

之前几篇随笔介绍客户端与服务器socket通信中,客户端断开连接时,服务器能知晓。但服务器进程断开时,客户端进程是无感知的,除非再一次发送数据至服务器才能知道服务器进程断开连接了。

本次通过对客户端程序修改,当服务器进程断开连接,客户端进程可以立刻知晓。

四、参考引用

UNIX网络编程 卷1:套接字联网API(第三版)

热门相关:仙城纪   横行霸道   豪门重生盛世闲女      最强反套路系统