1.接受tcp数据
#include <sys/socket.h> ssize_t recv(int socket, void *buffer, size_t length, int flags);
socket:读取的socket文件描述符
buffer:缓冲区的位置
length:缓冲区的大小
flags:提供了额外的控制,有如下标志位:
MSG_PEEK:窥探读缓存中数据,此次读操作不会导致这些数据被清除
MSG_OOB:接收紧急数据(带外数据,out_of_band)
MSG_WAITALL:在读取到指定数量的字节后才返回
2.发送tcp数据
#include <sys/socket.h> ssize_t recv(int socket, const void *buffer, size_t length, int flags);
socket:写入的socket文件描述符
buffer:缓冲区的位置
length:缓冲区的大小
flags:提供了额外的控制,有如下标志位:
MSG_OOB:接收紧急数据(带外数据,out_of_band)
MSG_DONTROUTE:在送出分组时不使用网关.只有直接连接在网络上的主机 才能接收到数据.这个标志通常仅用于诊断和路由程序. 可路由的协议族才能使用这个标志;包套接字不可以.
MSG_DONTWAIT:使用非阻塞式操作;如果操作需要阻塞,将返回 EAGAIN 错误(也可以用 F_SETFL fcntl(2) 设置 O_NONBLOCK 实现这个功能.)
MSG_NOSIGNAL:当流式套接字的另一端中断连接时不发送 SIGPIPE 信号,但仍然返回 EPIPE 错误.
MSG_CONFIRM (仅用于Linux 2.3以上版本):通知链路层发生了转发过程:得到了另一端的成功应答. 如果链路层没有收到通知,它将按照常规探测网络上的相邻 主机(比如通过免费arp). 只能用于 SOCK_DGRAM 和 SOCK_RAW类型的套接字,且仅对IPv4和IPv6有效。
Demo
send:
#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 ); send( sockfd, normal_data, strlen( normal_data ), 0 ); } close( sockfd ); return 0; }
recv:
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> #include <assert.h> #include <stdio.h> #include <string.h> #include <errno.h> static bool stop = false; static void handle_term( int sig ) { stop = true; } int main( int argc, char* argv[] ) { signal( SIGTERM, handle_term ); if( argc <= 3 ) { printf( "usage: %s ip_address port_number backlog ", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); int backlog = atoi( argv[3] ); int sock = socket( PF_INET, SOCK_STREAM, 0 ); assert( sock >= 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 ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) ); assert( ret != -1 ); ret = listen( sock, backlog ); assert( ret != -1 ); while ( ! stop ) { struct sockaddr_in client; socklen_t client_addrlength = sizeof( client ); printf("bloack wait for accept client..... "); int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); if (connfd < 0) { printf( "errno is: %d ", errno ); } else { char remote[INET_ADDRSTRLEN ]; // 将二进制整数地址转为点分字符串形式,结果存在remote,INET_ADDRSTRLEN:16 IPV4地址大小 // 成功返回指向remote的地址,失败则返回NULL const char* ptr = inet_ntop( AF_INET, &client.sin_addr, remote, INET_ADDRSTRLEN ); // 将网络字节序转为主机字节序,网络字节序是大端, int port = ntohs( client.sin_port ); printf( "connected with ip: %s and port: %d ", ptr, port); printf( "remote: %p and ptr: %p ", remote, ptr); close(connfd); } sleep( 1 ); } close( sock ); return 0; }
out:
send: 127.0.0.1 1234 send oob data out recv: 127.0.0.1 1234 got 5 bytes of normal data '123ab' got 1 bytes of oob data 'c' got 3 bytes of normal data '123'
可以看到带外数据是c
抓包分析一下:紧急指针是3,带外数据是一个字节,所以是c