`

Socket Options

    博客分类:
  • Java
阅读更多

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算法是开启的。

 

0
1
分享到:
评论

相关推荐

    java socket 经典教程

    非阻塞模式可以通过设置套接字选项(如`SocketOptions.SO_TIMEOUT`)或者使用NIO(New IO)框架实现。 7. **套接字选项** - Java Socket提供了一系列的套接字选项,如`SO_LINGER`、`TCP_NODELAY`等,可以用来调整...

    .net socket

    11. **套接字选项(Socket Options)** - Socket对象有各种设置,如超时、缓冲区大小等,可以通过`Socket.SetSocketOption()`方法调整。 12. **异常处理** - 在进行Socket操作时,需要捕获并处理可能抛出的异常,...

    socket编程的几篇很好的文章.rar_UNIX_csocket_socket_socket linux_socket 文

    Linux系统下的Socket接口与UNIX系统基本一致,但有一些额外的特性,如套接字选项(socket options)和扩展功能。Linux的Socket API允许开发者实现更复杂的网络服务,如多路复用(select/poll/epoll)、非阻塞I/O、...

    rawsocket抓包侦听

    3. **套接字选项(Socket Options)**:设置套接字选项是控制套接字行为的关键。例如,使用`setsockopt`函数设置`SO_RCVBUF`可以调整接收缓冲区大小,影响数据包的处理速度。 4. **数据包捕获(Packet Capture)**...

    Python自学教程-05-socket的介绍.ev4.rar

    在Python中,还有一些高级的socket功能,如套接字选项(socket options)、多路复用(如select和poll)、套接字超时等,它们可以用来优化网络程序的性能和处理复杂情况。 最后,`Python自学教程-05-socket的介绍.ev...

    Socket编程计算机网络字数还是不够吗我日了

    Socket编程的知识点还包括:套接字选项(socket options)、套接字地址结构(sockaddr结构体)、网络字节序转换(big-endian和little-endian的问题,使用ntohl, ntohs, htonl, htons等函数)、以及如何处理阻塞与非...

    java socket

    非阻塞模式则可以通过设置Socket的SocketOption,例如`SocketOptions.O_NONBLOCK`。 9. **NIO(New Input/Output)** - Java NIO库提供了一种新的Socket API,通过Selector和Channel实现多路复用,提高服务器处理...

    socket简单编程

    此外,你可能还会接触到套接字选项(socket options),如设置超时、禁用Nagle算法等,这些都是优化Socket通信性能和行为的方式。熟悉这些选项可以帮助你更好地控制Socket的行为。 在实践过程中,你可以使用提供的...

    brew socket 应用

    9. **套接字选项(Socket Options)**:可以调整socket的行为,如`SO_REUSEADDR`, `SO_LINGER`, `TCP_NODELAY`等。 10. **安全通信**:使用SSL/TLS进行加密,确保数据在传输过程中的安全性。 在实际操作中,初学者...

    windows socket 编程资料

    10. **套接字选项(Socket Options)**:可以使用`setsockopt()`和`getsockopt()`函数来设置或查询套接字的特定选项,如超时设置、重传策略等。 这些是Windows Socket编程的一些核心概念。压缩包中的文件可能包含...

    Windows Socket进阶

    在高级主题中,可能会涉及到套接字选项(socket options)的设置,如SO_REUSEADDR允许快速重启服务,SO_LINGER控制关闭连接时的数据发送,以及TCP_NODELAY禁用Nagle算法以减少延迟。此外,还有异步I/O和完成端口...

    VC套接字选项—修改缓冲区大小

    首先,了解套接字选项(Socket Options)。在Windows环境下,我们可以使用WSASetSocketOption函数来设置套接字选项。该函数接受四个参数:套接字句柄、选项层级、选项名和选项值。其中,选项层级通常为SOL_SOCKET,...

    UNIX Network Programming Volume 1, Third Edition (Unix网络编程卷1第3版英文版)

    Generic Socket Options Section 7.6. IPv4 Socket Options Section 7.7. ICMPv6 Socket Option Section 7.8. IPv6 Socket Options Section 7.9. TCP Socket Options Section 7.10. SCTP Socket Options ...

    Android安卓使用iosocket进行Socket通信简单封装优雅写代码

    socket = IO.socket(serverUrl, options); socket.connect(); } } ``` 2. 连接和断开Socket: ```java public static void connect() { if (socket != null && !socket.connected()) { socket.connect(); ...

    TCP.rar_C#TCP网口_网口_网口通信_网口通信程序

    9. **套接字选项(Socket Options)**:Socket类还有一些可配置的选项,如KeepAlive、NoDelay等,它们可以优化网络连接的行为。 通过这个"TCP.rar"压缩包,初学者可以学习到如何使用C#构建TCP客户端和服务器,理解...

    Android ipv6 chat

    当处理IPv6时,需要设置Socket的套接字选项为SocketOptions.IPPROTO_IPV6,并启用IPV6_V6ONLY选项,以确保Socket既可以连接到IPv4也可以连接到IPv6的服务器。如果只需要支持IPv6,可以不设置这个选项。 对于服务器...

    VC++制作一个Sniffer实例

    printf("Failed to set socket options\n"); closesocket(snifferSocket); WSACleanup(); return 1; } while (true) { char buffer[4096]; int bytesReceived = recvfrom(snifferSocket, buffer, sizeof...

    自用Android长连接

    2. 使用SocketOptions设置超时,避免无限等待。 3. 使用KeepAlive机制在设备休眠时保持网络连接。 4. 使用ConnectionPool管理多个连接,提高效率。 5. 考虑使用NIO(非阻塞I/O)或Netty等高性能网络库。 总结,...

Global site tag (gtag.js) - Google Analytics