带外数据比普通数据具有更高的优先级,TCP没有真正的带外数据,而是提供了一个我们要讨论的紧急模式,TCP将数据放置在套机口发送缓冲区的下一个可用位置,并设置这个连接的TCP紧急指针(urgent pointer)为下一个可用位置,TCP紧急指针有一个比用MSG_OOB标志写入的数据多一个字节的序列号。由于接收端的带外缓冲只有1B所以发送端发送的多字节带外数据只有最后1B被当做带外数据,若接收端TCP连接设置了SO_OOBINLINE选项则带外数据和普通数据一样存放于TCP接收缓冲区中。带外数据的发送与接收可以调用含有MSG_OOB标志的send和recv。sockatmark(int sockfd)可以判断sockfd下一个数据是否含有带外数据,若有就可以调用含有MSG_OOB标志的recv接收带外数据。接收带外数据的方式是带有MSG_OOB的recv接收,那么检测带外数据到来的方法:1 sockatmark、2 由于带外数据对于select返回是异常事件所以可以由select返回判断带外数据到来、3 通过SIG_URG信号处理函数
发送带外数据的客户端程序:
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> int main( int argc, char* argv[] ) { if( argc <= 2 ) { printf( "usage: %s ip_address port_number ", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); struct sockaddr_in server_address; bzero( &server_address, sizeof( server_address ) ); server_address.sin_family = AF_INET; inet_pton( AF_INET, ip, &server_address.sin_addr ); server_address.sin_port = htons( port ); int sockfd = socket( PF_INET, SOCK_STREAM, 0 ); assert( sockfd >= 0 ); if ( connect( sockfd, ( struct sockaddr* )&server_address, sizeof( server_address ) ) < 0 ) { printf( "connection failed " ); } else { printf( "send oob data out " ); const char* oob_data = "abc"; const char* normal_data = "123"; send( sockfd, normal_data, strlen( normal_data ), 0 ); send( sockfd, oob_data, strlen( oob_data ), MSG_OOB );//MSG_OOB标志的send发送带外数据,注意带外数据,接收端的带外缓存只有1B所以只有c是真正的带外数据,同理带MSG_OOB标志的recv能接收带外数据 send( sockfd, normal_data, strlen( normal_data ), 0 );//发送普通数据 } close( sockfd ); return 0; }
带MSG_OOB标志的recv
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #define BUF_SIZE 1024 int main( int argc, char* argv[] ) { if( argc <= 2 ) { printf( "usage: %s ip_address port_number ", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); 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 sock = socket( PF_INET, SOCK_STREAM, 0 ); assert( sock >= 0 ); int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) ); assert( ret != -1 ); ret = listen( sock, 5 ); assert( ret != -1 ); struct sockaddr_in client; socklen_t client_addrlength = sizeof( client ); int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); if ( connfd < 0 ) { printf( "errno is: %d ", errno ); } else { char buffer[ BUF_SIZE ]; memset( buffer, ' ', BUF_SIZE ); ret = recv( connfd, buffer, BUF_SIZE-1, 0 ); printf( "got %d bytes of normal data '%s' ", ret, buffer ); memset( buffer, ' ', BUF_SIZE ); ret = recv( connfd, buffer, BUF_SIZE-1, MSG_OOB );//带MSG_OOB标志的recv函数能接收带外数据 printf( "got %d bytes of oob data '%s' ", ret, buffer ); memset( buffer, ' ', BUF_SIZE ); ret = recv( connfd, buffer, BUF_SIZE-1, 0 ); printf( "got %d bytes of normal data '%s' ", ret, buffer ); close( connfd ); } close( sock ); return 0; }
通过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 ", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); printf( "ip is %s and port is %d ", ip, port ); 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 ", errno ); close( listenfd ); } char remote_addr[INET_ADDRSTRLEN]; //printf( "connected with ip: %s and port: %d ", 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, ' ', sizeof( buf ) ); FD_SET( connfd, &read_fds );//将文件描述符connfd和可读事件集合绑定,由于select内核在线修改事件集合以通知应用程序就绪事件,所以下次select调用前都需要重新绑定 FD_SET( connfd, &exception_fds ); ret = select( connfd + 1, &read_fds, NULL, &exception_fds, NULL );//select无限期等待事件就绪 //printf( "select one " ); 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 ", ret, buf ); } else if( FD_ISSET( connfd, &exception_fds ) )//处理异常事件,这里表示客户端发送带外数据到来 { ret = recv( connfd, buf, sizeof( buf )-1, MSG_OOB );//MSG_OOB标志 if( ret <= 0 ) { break; } printf( "get %d bytes of oob data: %s ", ret, buf ); } } close( connfd ); close( listenfd ); return 0; }
采用SIGURG信号检测带外数据的到来
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <signal.h> #include <fcntl.h> #define BUF_SIZE 1024//缓冲区大小 static int connfd; void sig_urg( int sig )//信号处理函数(SIGURG信号处理带外数据) { int save_errno = errno; char buffer[ BUF_SIZE ]; memset( buffer, ' ', BUF_SIZE ); int ret = recv( connfd, buffer, BUF_SIZE-1, MSG_OOB );//接收带外数据 printf( "got %d bytes of oob data '%s' ", ret, buffer );//输出带外数据 errno = save_errno; } void addsig( int sig, void ( *sig_handler )( int ) )//信号安装 { struct sigaction sa;//sigaction结构体 memset( &sa, ' ', sizeof( sa ) ); sa.sa_handler = sig_handler; sa.sa_flags |= SA_RESTART;//被信号中断的系统调用自动重启 sigfillset( &sa.sa_mask );//设置全部信号为进程信号掩码 assert( sigaction( sig, &sa, NULL ) != -1 );//信号处理 } int main( int argc, char* argv[] ) { if( argc <= 2 ) { printf( "usage: %s ip_address port_number ", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); 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 sock = socket( PF_INET, SOCK_STREAM, 0 ); assert( sock >= 0 ); int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) ); assert( ret != -1 ); ret = listen( sock, 5 ); assert( ret != -1 ); struct sockaddr_in client;//客户端地址 socklen_t client_addrlength = sizeof( client ); connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );//建立客户端连接 if ( connfd < 0 ) { printf( "errno is: %d ", errno ); } else { addsig( SIGURG, sig_urg );//添加SIGURG信号 fcntl( connfd, F_SETOWN, getpid() );//SIGURG的前提条件是进程必须持有文件描述符connfd char buffer[ BUF_SIZE ]; while( 1 ) { memset( buffer, ' ', BUF_SIZE ); ret = recv( connfd, buffer, BUF_SIZE-1, 0 );//接收客户端发送来的数据,若有带外数据到来则激活SIGURG信号处理函数 if( ret <= 0 ) { break; } printf( "got %d bytes of normal data '%s' ", ret, buffer ); } close( connfd ); } close( sock ); return 0; }