select同时处理普通数据及带外数据

select系统调用,同时处理普通数据及带外数据程序源码:

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#incldue <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("args err");
        return 1;
    }

    const char* ip = argv[1];
    int port =  atoi(argv[2]);

    int ret = 0;
    struct sockaddr_in address;
    bzero(&address, sizeof(address));
    address.sin_family = AF_INET;
    inet_pton(AF_INET, ip, &address.sin_addr);
    address.sin_port = htons(port);

    int listenfd = socket(PF_INET, SOCK_STREAM, 0);
    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:%d", errno);
        close(listenfd);
    }

    char buf[1024];
    fd_set read_fds;
    fd_set exception_fds;
    FD_ZERO(&read_fds);
    FD_ZERO(&exception_fds);

    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);
        if(ret < 0){
            printf("selection failure\n");
            break;
        }

        if(FD_ISSET(connfd, &read_fds))
        {
            //可读事件,采用普通recv函数读取数据
            ret = recv(connfd, buf, sizeof(buf)-1, 0);//最后一个参数为0
            if(ret < 0){
                break;
            }
            printf("获取%d的数据:%s\n", ret, buf);
        }
        else if(FD_ISSET(connfd, &exception_fds))//
        {
            //异常事件,采用带MSG_OOB标志的recv函数读取带外数据
            ret = recv(connfd, buf, sizeof(buf)-1, MSG_OOB);//最后一个参数为MSG_OOB
            if(ret <= 0){
                break;
            }
            printf("获取%d的数据:%s\n", ret, buf);
        }
    }

    close(connfd);
    close(listenfd);

    return 0;
}

代码来自:《Linux高性能服务器编程》

你可能感兴趣的:(select,MSG-OOB)