socket選項(xiàng)函數(shù)
功能:用來(lái)讀取和設(shè)置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選項(xiàng)表如下:
getsockopt和setsockopt 這兩個(gè)函數(shù)成功時(shí)返回0,失敗時(shí)返回-1并設(shè)置errno。
對(duì)于服務(wù)器而言,有部分socket選項(xiàng)只能在調(diào)用listen系統(tǒng)調(diào)用前針對(duì)監(jiān)聽(tīng)socket設(shè)置才有效。這是因?yàn)檫B接socket只能由accept調(diào)用返回,而accept從listen監(jiān)聽(tīng)隊(duì)列接受的連接至少已經(jīng)完成了TCP三次握手的前兩個(gè)步驟(因?yàn)閘isten監(jiān)聽(tīng)隊(duì)列中的連接至少已進(jìn)入SYN_RCVD狀態(tài)),這說(shuō)明服務(wù)器已經(jīng)往被接收連接上發(fā)送出了TCP同步報(bào)文段。但有的socket選項(xiàng)卻應(yīng)該在TCP同步報(bào)文段中設(shè)置,比如TCP最大報(bào)文段選項(xiàng)。對(duì)這種情況,linux給開(kāi)發(fā)人員提供的解決方案是:對(duì)監(jiān)聽(tīng)socket設(shè)置這些socket選項(xiàng),那么accept返回的連接socket將自動(dòng)繼承這些選項(xiàng)。這些選項(xiàng)包括:SO_DEBUG、SO_DONTROUTE、SO_KEEPALIVE、SO_LINGER、SO_OOBINLINE、SO_RCVBUF、SO_RCVLOWAT、SO_SNDBUF、SO_SNDLOWAT、TCP_MAXSEG和TCP_NODELAY。
對(duì)于客戶端而言,這些socket選項(xiàng)則應(yīng)該在調(diào)用connect函數(shù)之前設(shè)置,因?yàn)閏onnect調(diào)用成功返回之后,TCP三次握手已完成。
SO_REUSEADDR選項(xiàng)
前面討論過(guò)TCP連接的TIME_WAIT狀態(tài),并提到服務(wù)器程序可以通過(guò)設(shè)置socket選項(xiàng)SO_REUSEADDR來(lái)強(qiáng)制使用被處于TIME_WAIT狀態(tài)的連接占用的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;
}
經(jīng)過(guò)setsocketopt的設(shè)置之后,即使sock處于TIME_WAIT狀態(tài),與之綁定的socket地址也可以立即被重用。此外,我們也可以通過(guò)修改內(nèi)核參數(shù)/proc/sys/net/ipv4/tcp_tw_recycle 來(lái)快速回收被關(guān)閉的socket,從而使得TCP連接根本就不進(jìn)入TIME_WAIT狀態(tài),進(jìn)而允許應(yīng)用程序立即重用本地的socket地址。
SO_RCVBUF和SO_SNDBUF選項(xiàng)
SO_RCVBUF和SO_SNDBUF選項(xiàng)分別表示TCP接收緩沖區(qū)和發(fā)送緩沖區(qū)的大小。不過(guò),當(dāng)我們用setsockopt來(lái)設(shè)置TCP的接收緩沖區(qū)和發(fā)送緩沖區(qū)的大小時(shí),系統(tǒng)都會(huì)將其值加倍,并且不得小于其個(gè)最小值。TCP接收緩沖區(qū)的最小值是256字節(jié),而發(fā)送緩沖區(qū)的最小值是2048字節(jié)(不過(guò),不同的系統(tǒng)可能有不同的默認(rèn)最小值)。此外,我們可以直接修改內(nèi)核參數(shù)/proc/sys/net/ipv4/tcp_rmem和/proc/sys/net/ipv4/tcp_wmem來(lái)強(qiáng)制TCP接收緩沖區(qū)和發(fā)送緩沖區(qū)的大小沒(méi)有最小值限制。
修改TCP發(fā)送緩沖區(qū)的客戶端程序:
#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接收緩沖區(qū)的服務(wù)器程序:
#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;
}
運(yùn)行結(jié)果:
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
如上說(shuō)明:當(dāng)我們用setsockopt來(lái)設(shè)置TCP的接收緩沖區(qū)和發(fā)送緩沖區(qū)的大小時(shí),系統(tǒng)都會(huì)將其值加倍,并且不得小于其個(gè)最小值。
SO_RCVLOWAT和SO_SNDLOWAT選項(xiàng)
- SO_RCVLOWAT和SO_SNDLOWAT選項(xiàng)分別表示TCP接收緩沖區(qū)和發(fā)送緩沖區(qū)的低水位標(biāo)記。它們一般被I/O復(fù)用系統(tǒng)調(diào)用,用來(lái)判斷socket是否可讀或可寫(xiě)。當(dāng)TCP接收緩沖區(qū)中可讀數(shù)據(jù)的總數(shù)大于其低水位標(biāo)記時(shí),I/O復(fù)用系統(tǒng)調(diào)用將通知應(yīng)用程序可以從對(duì)應(yīng)的socket上讀取數(shù)據(jù);當(dāng)TCP發(fā)送緩沖區(qū)中的空閑空間(可以寫(xiě)入數(shù)據(jù)的空間)大于其低水位標(biāo)記時(shí),I/O復(fù)用系統(tǒng)調(diào)用將通知應(yīng)用程序可以往對(duì)應(yīng)的socket上寫(xiě)入數(shù)據(jù)。
- 默認(rèn)情況下,TCP接收緩沖區(qū)的低水位標(biāo)記和TCP發(fā)送緩沖區(qū)的低水位標(biāo)記均為1字節(jié)。
SO_LINGER選項(xiàng)
SO_LINGER選項(xiàng)用于控制close系統(tǒng)調(diào)用在關(guān)閉TCP連接時(shí)的行為。默認(rèn)情況下,當(dāng)我們使用close系統(tǒng)調(diào)用來(lái)關(guān)閉一個(gè)socket時(shí),close將立即返回,TCP模塊負(fù)責(zé)把該socket對(duì)應(yīng)的TCP發(fā)送緩沖區(qū)中殘留的數(shù)據(jù)發(fā)送給對(duì)方。
設(shè)置SO_LINGER選項(xiàng)的值時(shí),我們需要給setsockopt(getsockopt)系統(tǒng)調(diào)用傳遞一個(gè)linger類(lèi)型的結(jié)構(gòu)體,其定義如下:
#include <sys/socket.h>
struct linger
{
int l_onoff; //開(kāi)啟(非0)還是關(guān)閉(0)該選項(xiàng)
int l_linger; // 滯留時(shí)間
};
- 根據(jù)linger結(jié)構(gòu)體中兩個(gè)成員變量的不同值,close 系統(tǒng)調(diào)用可能產(chǎn)生如下3種行為之一:
- l_onoff 等于0。此時(shí)SO_LINGER選項(xiàng)不起作用,close用默認(rèn)行為關(guān)閉socket。
- l_onoff 不為0,l_linger等于0. 此時(shí)close 系統(tǒng)調(diào)用立即返回,TCP模塊將丟棄被關(guān)閉的socket對(duì)應(yīng)的TCP發(fā)送緩沖區(qū)中殘留的數(shù)據(jù),同時(shí)給對(duì)方一個(gè)復(fù)位報(bào)文段。因此,這種情況給服務(wù)器提供了異常終止一個(gè)連接的方法。l_onoff不為0,l_linger大于0 。此時(shí)close的行為取決于兩個(gè)條件:(1)被關(guān)閉的socket對(duì)應(yīng)的TCP發(fā)送緩沖區(qū)中是否還有殘留的數(shù)據(jù);(2)該socket是阻塞的還是非阻塞的。 對(duì)于阻塞的socket,close將等待一段長(zhǎng)為l_linger的時(shí)間,直到TCP模塊發(fā)送完所有殘留數(shù)據(jù)并得到對(duì)方的確認(rèn)。如果這段之間內(nèi)TCP模塊沒(méi)有發(fā)送完殘留數(shù)據(jù)并得到對(duì)方的確認(rèn),那么close系統(tǒng)調(diào)用將返回-1并設(shè)置errno為EWOULDBLOCK。 如果socket是非阻塞的,close將立即返回,此時(shí)我們需要根據(jù)其返回值和errno來(lái)判斷殘留數(shù)據(jù)是否已經(jīng)發(fā)送完畢。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。