【Java 网络编程】服务器端 ServerSocket 配置 ( 端口复用 | 缓冲区设置 | 超时时间 | 性能权重 | 端口绑定 )



I ServerSocket 端口号绑定参数



1. 建议绑定时机 : 绑定端口号的操作建议在设置的最后一步进行操作 , 如果绑定了端口号 , 很多设置就无效了 ;

2. int backlog 参数作用 : 创建 ServerSocket 对象之后 , 需要绑定本地的 IP 地址和端口号 , 服务器套接字绑定方法还有一个 int backlog 参数 , 这个参数指的是允许等待的连接队列 , 如将该值设置成 10 的效果是 , 当客户端连接服务器 , 但是服务器还没有调用 accept 方法接收客户端的连接 , 此时如果有 10 个以内的客户端连接 , 这 10 个连接都在缓冲区中等待 , 如果出现第 11 个客户端连接 , 此时客户端就会抛出异常 ; 注意这个异常是在客户端触发的 ;

//服务器端绑定本地的 IP 地址和端口号
serverSocket.bind(new InetSocketAddress(Inet4Address.getLocalHost(), 8888), 10);


II ServerSocket 复用绑定端口设置



设置是否可以复用 ServerSocket 绑定的地址和端口号 : setReuseAddress( true ) ;

serverSocket.setReuseAddress(true);

Socket 连接在建立时 , 会使用之前绑定本地的 IP 地址和端口号 , 这个端口号在使用之后 , 2 分钟之内不允许再次使用 ; 进行了该设置之后 , 可以在连接关闭之后 , 马上使用该本地 IP 地址和端口号 ;



III ServerSocket 设置缓冲区大小



1. 缓冲区大小设置 : ServerSocket 只有接收缓冲区设置 , 其原理与 Socket 缓冲区原理相同 ;

  • ① 接收缓冲区设置 ;
serverSocket.setReceiveBufferSize(64 * 1024 * 1024);

2. 设置时机 : 注意设置缓冲区一定要在 accept 之前进行设置 , 如果在连接建立之后设置该缓冲区是无效的 ;



IV ServerSocket 设置超时时间



1. 设置 ServerSocket 超时时间 , 该超时时间没有实际的概念 , 用于设置与阻塞相关操作的超时时间 , ServerSocket 中只有 accept 操作会有阻塞 , 设置了 2 秒阻塞时间 , 如果 accept 阻塞超过 2000 毫秒 , 就会抛出异常 , 此时可以捕获该异常继续等待 2 秒 ;

serverSocket.setSoTimeout(2000);

一般情况下不设置该超时时间 , 即服务器端永久等待客户端连接

2. 单位 : 毫秒 ( ms ) ;



V ServerSocket 设置性能参数



ServerSocket 调用 setPerformancePreferences 设置性能参数 , 与 Socket 调用 setPerformancePreferences 设置原理是一样的 ;

//对延迟性能要求最高
serverSocket.setPerformancePreferences(1, 10, 1);

1. 调用 ServerSocket 对象的 setPerformancePreferences 方法 , 设置连接的性能参数 ; 连接有以下三个性能参数 :

  • ① 连接时间 ;
  • ② 往返延迟 ;
  • ③ 带宽 ;

2. 设置的是权重不是具体性能参数 : 设置的值不是具体的参数 , 而是连接的性能权重 , 对哪个性能要求比较高 ;

3. 连接时间 : 如果该 Socket 的连接很频繁 , 连接后传一个数据 , 马上断开 , 这时候比较看重连接时间性能 , 此时可以将第一个参数设置成 10 , 后两个参数设置成 1 , 表示注重连接时间性能 ;

//设置 连接时间 性能参数较重要
socket.setPerformancePreferences(10, 1, 1);

4. 往返延迟 : 如果开发的是网游服务器 , 此时对延迟很看重 , 这时候可以将第二个参数设置成比较高的权重 ;

//设置 往返延迟 性能参数较重要
socket.setPerformancePreferences(1, 10, 1);

5. 带宽 : 如果开发的是音视频服务器 , 注重带宽性能 , 此时需要将第三个参数设置成较高的权重 ;

//设置 带宽 性能参数较重要
socket.setPerformancePreferences(1, 10, 1);

6. 上面的延迟和带宽的性能是互斥的 , 延迟低 , 就意味着很小的包就要发送一次 , 其带宽就低了 , 延迟高了 , 每次积累很多数据才发送 , 其带宽就相应的提高了 ;

7. 函数原型 :

    /**
     * Sets performance preferences for this socket.
     *
     * <p> Sockets use the TCP/IP protocol by default.  Some implementations
     * may offer alternative protocols which have different performance
     * characteristics than TCP/IP.  This method allows the application to
     * express its own preferences as to how these tradeoffs should be made
     * when the implementation chooses from the available protocols.
     *
     * <p> Performance preferences are described by three integers
     * whose values indicate the relative importance of short connection time,
     * low latency, and high bandwidth.  The absolute values of the integers
     * are irrelevant; in order to choose a protocol the values are simply
     * compared, with larger values indicating stronger preferences. Negative
     * values represent a lower priority than positive values. If the
     * application prefers short connection time over both low latency and high
     * bandwidth, for example, then it could invoke this method with the values
     * {@code (1, 0, 0)}.  If the application prefers high bandwidth above low
     * latency, and low latency above short connection time, then it could
     * invoke this method with the values {@code (0, 1, 2)}.
     *
     * <p> Invoking this method after this socket has been connected
     * will have no effect.
     *
     * @param  connectionTime
     *         An {@code int} expressing the relative importance of a short
     *         connection time
     *
     * @param  latency
     *         An {@code int} expressing the relative importance of low
     *         latency
     *
     * @param  bandwidth
     *         An {@code int} expressing the relative importance of high
     *         bandwidth
     *
     * @since 1.5
     */
    public void setPerformancePreferences(int connectionTime,
                                          int latency,
                                          int bandwidth)
发布了252 篇原创文章 · 获赞 1013 · 访问量 168万+

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/100540024