Accept common data and out-of-band data simultaneously under Linux

Source: Internet
Author: User

#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,&AMP;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, ' n ', 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;}

Accept common data and out-of-band data simultaneously under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.