【Java 网络编程】服务器端 ServerSocket 配置 ( 端口复用 | 缓冲区设置 | 超时时间 | 性能权重 | 端口绑定 )
文章目錄
- I ServerSocket 端口號綁定參數
- II ServerSocket 復用綁定端口設置
- III ServerSocket 設置緩沖區大小
- IV ServerSocket 設置超時時間
- V 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 緩沖區原理相同 ;
- ① 接收緩沖區設置 ;
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) 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的【Java 网络编程】服务器端 ServerSocket 配置 ( 端口复用 | 缓冲区设置 | 超时时间 | 性能权重 | 端口绑定 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java 网络编程】客户端 Socke
- 下一篇: 【Java 网络编程】TCP 数据传输示