IO多路复用select函数

功能:阻塞函数,阻塞等待要监听的集合中的文件描述符准备就绪,如果有文件描述符准备就绪,则当前函数立即解除阻塞.
原型:
       /* According to POSIX.1-2001, POSIX.1-2008 */
       #include <sys/select.h>

       /* According to earlier standards */
       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       int select(int nfds, fd_set *readfds, fd_set *writefds,
                  fd_set *exceptfds, struct timeval *timeout);
参数:
    int nfds:参数中三个集合中,最大的文件描述符+1;
    fd_set *readfds, fd_set *writefds,
 fd_set *exceptfds:读集合,写集合,其他集合。如果不使用某个集合,则填NULL;
    struct timeval *timeout:超时时间。如果不想设置超时时间,可以填NULL,此时该函数会一直阻塞,直到有文件描述符准备就绪。
           struct timeval {
               long    tv_sec;         /* seconds */
               long    tv_usec;        /* microseconds */
           };
返回值:
    >0 , 成功,返回成功产生事件的文件描述符个数;
    =0,  超时了。
    =-1, 失败,更新errno;
    
       void FD_CLR(int fd, fd_set *set);        将fd从集合中删除
       int  FD_ISSET(int fd, fd_set *set);      判断fd是否在集合中,存在返回真,不存在返回假
       void FD_SET(int fd, fd_set *set);        将fd添加到集合中
       void FD_ZERO(fd_set *set);               清空集合

使用select函数 实现简单服务器和客户端 

服务端支持并发连接,同时支持终端输入信息发送给客户端

客户端支持显示接收到的服务端消息, 同时支持终端输入消息发送服务器

server.c

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

#define PRINT_ERROR(msg) do{\
    fprintf(stderr, "line:__%d__ ", __LINE__);\
    perror(msg);\
    exit(-1);\
}while(0)

int main(int argc, const char *argv[]) {
    int sfd;
    if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        PRINT_ERROR("socket");
    }
    int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
        PRINT_ERROR("setsockopt");
    }
    struct sockaddr_in sin;
    sin.sin_family         = AF_INET;
    sin.sin_port         = htons(atoi(argv[2]));
    sin.sin_addr.s_addr = inet_addr(argv[1]);
    if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        PRINT_ERROR("bind");
    }
    if(listen(sfd, 10) < 0) {
        PRINT_ERROR("listen");
    }
    int max_fd = 0;
    int accept_fd = 0;
    int nbytes = 0;
    char buf[128] = {0};
    fd_set readfds;
    fd_set readfds_temps;
    FD_ZERO(&readfds);
    FD_ZERO(&readfds_temps);
    FD_SET(sfd, &readfds);
    FD_SET(0, &readfds);

    max_fd = max_fd>sfd?max_fd:sfd;
    int fd_res = 0;
    struct timeval tm;
    while (1) {
        tm.tv_sec = 5;
        tm.tv_usec = 0;
        readfds_temps = readfds;
        if ((fd_res = select(max_fd + 1, &readfds_temps, NULL, NULL, &tm)) == -1) {
            PRINT_ERROR("select");
        } else if (fd_res == 0) {
            printf("time out...\n");
        }
        for (int i = 0; i <= max_fd && fd_res != 0; i++) {
            if (FD_ISSET(i, &readfds_temps)) {
                if (i == sfd) {
                    if ((accept_fd = accept(sfd, NULL, NULL)) == -1) {
                        PRINT_ERROR("accept error");
                    }
                    printf("client [%d] connect!!!\n", accept_fd);
                    FD_SET(accept_fd, &readfds);
                    max_fd = max_fd>accept_fd?max_fd:accept_fd;
                } else if (i == 0) {
                    int fd = 0;
                    char input_buf[128] = {0};
                    scanf("%d %s", &fd, input_buf);
                    if (send(fd, input_buf, sizeof(input_buf), 0) == -1) {
                        PRINT_ERROR("send error");
                    }
                } else {
                    if ((nbytes = recv(i, buf, sizeof(buf), 0)) == -1) {
                        PRINT_ERROR("recv error");
                    } else if (nbytes == 0) {
                        printf("client [%d] disconnect !!!\n", i);
                        FD_CLR(i, &readfds);
                        close(i);
                        continue;
                    }
                    if (!strncmp(buf, "quit", 4)) {
                        printf("client [%d] disconnect !!!\n", i);
                        FD_CLR(i, &readfds);
                        close(i);
                        continue;
                    }
                    strcat(buf, "---hi !!!");
                    if (send(i, buf, sizeof(buf), 0) == -1) {
                        PRINT_ERROR("send");
                    }
                }
                fd_res --;
            }
        }
    }
    close(sfd);
    return 0;
}

client.c

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

#define PRINT_ERROR(msg) do{\
    fprintf(stderr, "line:__%d__ ", __LINE__);\
    perror(msg);\
    exit(-1);\
}while(0)

int main(int argc, const char *argv[]) {
    int sfd;
    if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        PRINT_ERROR("socket");
    }
    struct sockaddr_in serveraddr;
    serveraddr.sin_family         = AF_INET;
    serveraddr.sin_port         = htons(atoi(argv[2]));
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
    socklen_t serveraddr_len = sizeof(serveraddr);
    if (connect(sfd, (struct sockaddr *)&serveraddr, serveraddr_len) == -1) {
        PRINT_ERROR("connect error");
    }
    int max_fd = 0;
    int accept_fd = 0;
    int nbytes = 0;
    char buf[128] = {0};
    fd_set readfds;
    fd_set readfds_temps;
    FD_ZERO(&readfds);
    FD_ZERO(&readfds_temps);
    FD_SET(0, &readfds);
    FD_SET(sfd, &readfds);

    max_fd = max_fd>sfd?max_fd:sfd;
    int fd_res = 0;
    struct timeval tm;
    while (1) {
        tm.tv_sec = 5;
        tm.tv_usec = 0;
        readfds_temps = readfds;
        if ((fd_res = select(max_fd + 1, &readfds_temps, NULL, NULL, &tm)) == -1) {
            PRINT_ERROR("select");
        } else if (fd_res == 0) {
            printf("time out...\n");
        }
        for (int i = 0; i <= max_fd && fd_res != 0; i++) {
            memset(buf, 0, sizeof(buf));
            if (FD_ISSET(i, &readfds_temps)) {
                if (i == 0) {
                    fgets(buf, sizeof(buf), stdin);
                    buf[strlen(buf) - 1] = '\0';
                    if (send(sfd, buf, sizeof(buf), 0) == -1) {
                        PRINT_ERROR("send");
                    }
                    if (!strncmp(buf, "quit", 4)) {
                        close(sfd); 
                        return 0;
                    }
                } else {
                    if ((nbytes = recv(i, buf, sizeof(buf), 0)) == -1) {
                        PRINT_ERROR("recv error");
                    }
                    printf("receive ----> %s\n", buf);
                }
                fd_res --;
            }
        }
    }
    return 0;
}

执行效果

server

ubuntu@ubuntu: ~$ ./server 127.0.0.1 8888
client [4] connect!!!
time out...
4 hello
4 nihao
client [5] connect!!!
time out...
5 hello
5 world
client [4] disconnect !!!
client [5] disconnect !!!
^C
ubuntu@ubuntu: ~$

client1

ubuntu@ubuntu: ~$ ./client 127.0.0.1 8888
1
receive ----> 1---hi !!!
2
receive ----> 2---hi !!!
3
receive ----> 3---hi !!!
time out...
receive ----> hello
receive ----> nihao
time out...
time out...
time out...
quit
ubuntu@ubuntu: ~$ 

client2

ubuntu@ubuntu: ~$ ./client 127.0.0.1 8888
a
receive ----> a---hi !!!
b
receive ----> b---hi !!!
c
receive ----> c---hi !!!
time out...
receive ----> hello
receive ----> world
time out...
quit
ubuntu@ubuntu: ~$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值