欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Socket Options

程序员文章站 2024-01-31 20:26:58
...

Socket Options

以下出现中文的地方并不是对英文的翻译

 

1. SO_TIMEOUT

    单位是毫秒,表示等待客戶端连接的最长时间。

    Set a timeout on blocking Socket operations:

         ServerSocket.accept();

         SocketInputStream.read();

         DatagramSocket.receive();

    With this option set to a non-zero  timeout, a call to accept() for this ServerSocket  will block for only this amount of time.  If the timeout expires,  a java.net.SocketTimeoutException is raised, though the  ServerSocket is still valid.  

    The option must be enabled prior to entering the blocking operation to have effect.  The timeout must be  > 0.

 

 

2. SO_REUSEADDR

    是否允许重用服务器所绑定的地址

    When a TCP connection is closed the connection may remain in a timeout state for a period of time after the connection is closed (typically known as the TIME_WAIT state or 2MSLwait state,For applications using a well known socket address or port it may not be possible to bind a socket to the required SocketAddress if there is a connection in the timeout state involving the socket address or port.Enabling  SO_REUSEADDR prior to binding the socket using ServerSocket.bind(SocketAddress) allows the socket to be bound even though a previous connection is in a timeout state.

 

 

3. SO_RCVBUF

    接收数据的缓冲区的大小

    Sets a default proposed(建议的) value for the  {@link SocketOptions.SO_RCVBUF SO_RCVBUF} option for sockets accepted from this ServerSocket. The value actually set  in the accepted socket must be determined by calling Socket.getReceiveBufferSize() after the socket is returned by#accept().

    The value of SocketOptions.SO_RCVBUF SO_RCVBUF is used both to set the size of the internal socket receive buffer, and to set the size of the TCP receive window that is advertized to the remote peer.

    It is possible to change the value subsequently, by calling Socket.setReceiveBufferSize(int). However, if the application wishes to allow a receive window larger than 64K bytes, as defined by RFC1323 then the proposed value must be set in the ServerSocket before  it is bound to a local address. This implies, that the ServerSocket must be created with the no-argument constructor, then setReceiveBufferSize() must

be called and lastly the ServerSocket is bound to an address by calling bind().

 

 

4.SO_SNDBUF

    发送数据的缓冲区的大小

    Set a hint the size of the underlying buffers used by the platform for outgoing network I/O. When used in set, this is a  suggestion to the kernel from the application about the size of  buffers to use for the data to be sent over the socket. When used in get, this must return the size of the buffer actually used by the platform when sending out data on this socket.

 

 

5.SO_LINGER

    Specify a linger-on-close timeout.  This option disables/enables immediate return from a close() of a TCP Socket.  Enabling this option with a non-zero Integer timeout means that a close() will block pending the transmission and acknowledgement of all data written to the peer, at which point the socket is closed gracefully.  Upon reaching the linger timeout, the socket is closed forcefully, with a TCP RST. Enabling the option with a timeout of zero does a forceful close immediately. If the specified timeout value exceeds 65,535 it will be reduced to 65,535.

    在默认情况下,当调用close方法后,将立即返回;如果这时仍然有未被送出的数据包,那么这 些数据包将被丢弃。如果将linger参数设为一个正整数n时(n的值最大是65,535),在调用close方法后,将最多被阻塞n秒。在这n秒内,系 统将尽量将未送出的数据包发送出去;如果超过了n秒,如果还有未发送的数据包,这些数据包将全部被丢弃;而close方法会立即返回。如果将linger 设为0,和关闭SO_LINGER选项的作用是一样的

 

 

6.SO_KEEPALIVE

    When the keepalive option is set for a TCP socket and no data has been exchanged across the socket in either direction for 2 hours (NOTE: the actual value is implementation dependent),  TCP automatically sends a keepalive probe to the peer. This probe(探测) is a TCP segment to which the peer must respond.  One of three responses is expected:

     1. The peer responds with the expected ACK. The application is not notified (since everything is OK). TCP will send another probe following another 2 hours of inactivity.

     2. The peer responds with an RST, which tells the local TCP that the peer host has crashed and rebooted. The socket is closed.

     3. There is no response from the peer. The socket is closed.

 

    The purpose of this option is to detect if the peer host crashes.

 

 

7.SO_OOBINLINE

    When the OOBINLINE option is set, any TCP urgent data received on the socket will be received through the socket input stream. When the option is disabled (which is the default) urgent data  is silently discarded.

    如果这个Socket选项打开,可以通过Socket类的sendUrgentData方法向服务器发送一个单字节的数据。这个单字节数据并不经过 输出缓冲区,而是立即发出。虽然在客户端并不是使用OutputStream向服务器发送数据,但在服务端程序中这个单字节的数据是和其它的普通数据混在 一起的。因此,在服务端程序中并不知道由客户端发过来的数据是由OutputStream还是由sendUrgentData发过来的

 

 

8.TCP_NODELAY

        Disable Nagle's algorithm for this connection.  Written data to the network is not buffered pending acknowledgement of previously written data.

        Nagle's algorithm:在默认情况下,客户端向服务器发送数据时,会根据数据包的大小决定是否立即发送。当数据包中的数据很少时,如只有1个字节,而数据包的头却有几十个 字节(IP头+TCP头)时,系统会在发送之前先将较小的包合并到软大的包后,一起将数据发送出去。在发送下一个数据包时,系统会等待服务器对前一个数据 包的响应,当收到服务器的响应后,再发送下一个数据包,这就是所谓的Nagle算法;在默认情况下,Nagle算法是开启的。