#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選項表以下:linux
#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; }
修改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接收緩衝區的服務器程序: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> #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; }
運行結果:tcp
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_LINGER選項用於控制close系統調用在關閉TCP鏈接時的行爲。默認狀況下,當咱們使用close系統調用來關閉一個socket時,close將當即返回,TCP模塊負責把該socket對應的TCP發送緩衝區中殘留的數據發送給對方。
設置SO_LINGER選項的值時,咱們須要給setsockopt(getsockopt)系統調用傳遞一個linger類型的結構體,其定義以下:
spa
#include <sys/socket.h> struct linger { int l_onoff; //開啓(非0)仍是關閉(0)該選項 int l_linger; // 滯留時間 };
根據linger結構體中兩個成員變量的不一樣值,close 系統調用可能產生以下3種行爲之一:rest