I/O复用-select系统调用

I/O复用使得程序能够同时监听多个文件描述符,通常一下情况需要用到I/O复用技术:

1、客户端程序要同时处理多个socket。
2、客户端程序要同时处理用户输入和网络连接。
3、TCP服务器要同时处理监听socket和连接socket。
4、服务器要同时处理TCP请求和UDP请求。
5、服务器要同时监听多个端口,或者处理多种服务。

select系统调用

用途:在一段指定的时间内,监听用户感兴趣的文件描述符上的可读、可写和异常等事件。

#include <sys/select.h>
/*
 * nfds 参数指定被监听的文件描述符的总数,值为 select 监听的所有文件描述符中的最大值加 1. 
 * readfds、writefds 和 exceptfds 参数分别指向可读、可写和异常等事件对应的文件描述符集合。
 * timeout 参数用来设置 select 函数的超时时间。
 */
int select( int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout );

#include <sys/select.h>
FD_ZERO( fd_set *fdset );                   /* 清除 fdset 的所有位 */
FD_SET( int fd, fd_set *fdset );            /* 设置 fdset 的位 fd */
FD_CLR( int fd, fd_set *fdset );            /* 清除 fdset 的位 fd */
int FD_ISSET( int fd, fd_set *fdset );      /* 测试 fdset 的位 fd 是否被设置 */

select成功返回就绪(可读、可写和异常)文件描述符的总数,超时时间内没有任何文件描述符就绪的话就返回 0,失败返回 -1 并设置 errno 为 EINTR

文件描述符就绪的条件

socket 可读:
① socket 内核接收缓存区中的字节数大于或等于其低水位标记SO_RCVLOWAT.
② socket 通信的对方关闭连接.
③ 监听 socket 上有新的连接请求.
④ socket 上有未处理的错误.

socket 可写:
① socket 内核发送缓存区中的字节数大于或等于其低水位标记SO_RCVLOWAT.
② socket 的写操作被关闭.
③ socket 使用非阻塞 connnect 连接成功或者失败(超时)之后.
④ socket 上有未处理的错误.

socket 能处理的异常情况只有一种:
socket 上接收到带外数据.

带外数据就是与普通数据不同的通道独立传给用户,是相连的每一对流套接口一个逻辑上独立的传输通道。

socket 上接收到普通数据和带外数据都将使 select 返回,前者处于可读状态,后者处于异常状态。

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

int main( int argc, char* argv[] )
{
    if( argc <= 2 )
    {
        printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi( argv[2] );
    printf( "ip is %s and port is %d\n", ip, port );

    int ret = 0;
        struct sockaddr_in address;
        bzero( &address, sizeof( address ) );//将address 内的字节全部置为零
        address.sin_family = AF_INET;//AF_INET为IPV4网络协议的套接字类型
        inet_pton( AF_INET, ip, &address.sin_addr );
        address.sin_port = htons( port );//将整形变量从主机字节顺序变成网络字节顺序

    int listenfd = socket( PF_INET, SOCK_STREAM, 0 );
    assert( listenfd >= 0 );

        ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) );
    assert( ret != -1 );

    ret = listen( listenfd, 5 );
    assert( ret != -1 );

    struct sockaddr_in client_address;
        socklen_t client_addrlength = sizeof( client_address );
    int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength );
    if ( connfd < 0 )
    {
        printf( "errno is: %d\n", errno );
        close( listenfd );
    }

    char remote_addr[INET_ADDRSTRLEN];
    printf( "connected with ip: %s and port: %d\n", inet_ntop( AF_INET, &client_address.sin_addr, remote_addr, INET_ADDRSTRLEN ), ntohs( client_address.sin_port ) );

    char buf[1024];
        fd_set read_fds;
        fd_set exception_fds;

        FD_ZERO( &read_fds );
        FD_ZERO( &exception_fds );

        int nReuseAddr = 1;
    setsockopt( connfd, SOL_SOCKET, SO_OOBINLINE, &nReuseAddr, sizeof( nReuseAddr ) );
    while( 1 )
    {
        memset( buf, '\0', sizeof( buf ) );
        /* 每次调用 select 前都要重新在 read_fds 和 exception_fds 中设置文件描述符connfd,因为事件发生后,文件描述符集合将被内核修改 */
        FD_SET( connfd, &read_fds );
        FD_SET( connfd, &exception_fds );

            ret = select( connfd + 1, &read_fds, NULL, &exception_fds, NULL );
        printf( "select one\n" );
            if ( ret < 0 )
            {
                    printf( "selection failure\n" );
                    break;
            }
            /* 对于可读事件,采用普通的 recv 函数读取数据 */
            if ( FD_ISSET( connfd, &read_fds ) )
        {
                ret = recv( connfd, buf, sizeof( buf )-1, 0 );
            if( ret <= 0 )
            {
                break;
            }
            printf( "get %d bytes of normal data: %s\n", ret, buf );
        }
        /* 对于异常事件,采用带 MSG_OOB 标志的 recv 函数读取带外数据 */
        else if( FD_ISSET( connfd, &exception_fds ) )
            {
                ret = recv( connfd, buf, sizeof( buf )-1, MSG_OOB );
            if( ret <= 0 )
            {
                break;
            }
            printf( "get %d bytes of oob data: %s\n", ret, buf );
            }

    }

    close( connfd );
    close( listenfd );
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值