您好,登錄后才能下訂單哦!
socket選項函數
功能:用來讀取和設置socket文件描述符屬性的方法
#include <sys/scoket.h> int getsockopt ( int sockfd, int level, int option_name, void* option_value, socklen_t* restrict option_len ); int setsockopt ( int sockfd, int level, int option_name, const void* option_value, socklen_t option_len);
socket選項表如下:
getsockopt和setsockopt 這兩個函數成功時返回0,失敗時返回-1并設置errno。
對于服務器而言,有部分socket選項只能在調用listen系統調用前針對監聽socket設置才有效。這是因為連接socket只能由accept調用返回,而accept從listen監聽隊列接受的連接至少已經完成了TCP三次握手的前兩個步驟(因為listen監聽隊列中的連接至少已進入SYN_RCVD狀態),這說明服務器已經往被接收連接上發送出了TCP同步報文段。但有的socket選項卻應該在TCP同步報文段中設置,比如TCP最大報文段選項。對這種情況,linux給開發人員提供的解決方案是:對監聽socket設置這些socket選項,那么accept返回的連接socket將自動繼承這些選項。這些選項包括:SO_DEBUG、SO_DONTROUTE、SO_KEEPALIVE、SO_LINGER、SO_OOBINLINE、SO_RCVBUF、SO_RCVLOWAT、SO_SNDBUF、SO_SNDLOWAT、TCP_MAXSEG和TCP_NODELAY。
對于客戶端而言,這些socket選項則應該在調用connect函數之前設置,因為connect調用成功返回之后,TCP三次握手已完成。
SO_REUSEADDR選項
前面討論過TCP連接的TIME_WAIT狀態,并提到服務器程序可以通過設置socket選項SO_REUSEADDR來強制使用被處于TIME_WAIT狀態的連接占用的socket地址。
#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> int main( int argc, char* argv[] ) { if( argc <= 2 ) { printf( "usage: %s ip_address port_number\n", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); int sock = socket( PF_INET, SOCK_STREAM, 0 ); assert( sock >= 0 ); int reuse = 1; setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof( reuse ) ); 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, 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\n", errno ); } else { char remote[INET_ADDRSTRLEN ]; printf( "connected with ip: %s and port: %d\n", inet_ntop( AF_INET, &client.sin_addr, remote, INET_ADDRSTRLEN ), ntohs( client.sin_port ) ); close( connfd ); } close( sock ); return 0; }
經過setsocketopt的設置之后,即使sock處于TIME_WAIT狀態,與之綁定的socket地址也可以立即被重用。此外,我們也可以通過修改內核參數/proc/sys/net/ipv4/tcp_tw_recycle 來快速回收被關閉的socket,從而使得TCP連接根本就不進入TIME_WAIT狀態,進而允許應用程序立即重用本地的socket地址。
SO_RCVBUF和SO_SNDBUF選項
SO_RCVBUF和SO_SNDBUF選項分別表示TCP接收緩沖區和發送緩沖區的大小。不過,當我們用setsockopt來設置TCP的接收緩沖區和發送緩沖區的大小時,系統都會將其值加倍,并且不得小于其個最小值。TCP接收緩沖區的最小值是256字節,而發送緩沖區的最小值是2048字節(不過,不同的系統可能有不同的默認最小值)。此外,我們可以直接修改內核參數/proc/sys/net/ipv4/tcp_rmem和/proc/sys/net/ipv4/tcp_wmem來強制TCP接收緩沖區和發送緩沖區的大小沒有最小值限制。
修改TCP發送緩沖區的客戶端程序:
#include <sys/socket.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #define BUFFER_SIZE 512 int main( int argc, char* argv[] ) { if( argc <= 3 ) { printf( "usage: %s ip_address port_number send_bufer_size\n", 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 sock = socket( PF_INET, SOCK_STREAM, 0 ); assert( sock >= 0 ); int sendbuf = atoi( argv[3] ); int len = sizeof( sendbuf ); setsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, sizeof( sendbuf ) ); getsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, ( socklen_t* )&len ); printf( "the tcp send buffer size after setting is %d\n", sendbuf ); if ( connect( sock, ( struct sockaddr* )&server_address, sizeof( server_address ) ) != -1 ) { char buffer[ BUFFER_SIZE ]; memset( buffer, 'a', BUFFER_SIZE ); send( sock, buffer, BUFFER_SIZE, 0 ); } close( sock ); return 0; }
修改TCP接收緩沖區的服務器程序:
#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 BUFFER_SIZE 1024 int main( int argc, char* argv[] ) { if( argc <= 3 ) { printf( "usage: %s ip_address port_number receive_buffer_size\n", 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 recvbuf = atoi( argv[3] ); int len = sizeof( recvbuf ); setsockopt( sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, sizeof( recvbuf ) ); getsockopt( sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, ( socklen_t* )&len ); printf( "the receive buffer size after settting is %d\n", recvbuf ); 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\n", errno ); } else { char buffer[ BUFFER_SIZE ]; memset( buffer, '\0', BUFFER_SIZE ); while( recv( connfd, buffer, BUFFER_SIZE-1, 0 ) > 0 ){} close( connfd ); } close( sock ); return 0; }
運行結果:
root@iZbp1anc6yju2dks3nw5j0Z:~/test/socket# ./client 127.0.0.1 12345 2000
the tcp send buffer size after setting is 4608
root@iZbp1anc6yju2dks3nw5j0Z:~/test/socket# ./server 127.0.0.1 12345 50
the receive buffer size after settting is 2304
如上說明:當我們用setsockopt來設置TCP的接收緩沖區和發送緩沖區的大小時,系統都會將其值加倍,并且不得小于其個最小值。
SO_RCVLOWAT和SO_SNDLOWAT選項
SO_LINGER選項
SO_LINGER選項用于控制close系統調用在關閉TCP連接時的行為。默認情況下,當我們使用close系統調用來關閉一個socket時,close將立即返回,TCP模塊負責把該socket對應的TCP發送緩沖區中殘留的數據發送給對方。
設置SO_LINGER選項的值時,我們需要給setsockopt(getsockopt)系統調用傳遞一個linger類型的結構體,其定義如下:
#include <sys/socket.h> struct linger { int l_onoff; //開啟(非0)還是關閉(0)該選項 int l_linger; // 滯留時間 };
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。