Linux下同时接受普通数据和带外数据

#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()
{
    const char *ip="127.0.0.1";
    int port=12345;
    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(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 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));
        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");
            break;
        }
        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);
        }
        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;
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。