【Java 网络编程】服务器端 ServerSocket 配置 ( 端口复用 | 缓冲区设置 | 超时时间 | 性能权重 | 端口绑定 )
文章目錄
- I ServerSocket 端口號(hào)綁定參數(shù)
- II ServerSocket 復(fù)用綁定端口設(shè)置
- III ServerSocket 設(shè)置緩沖區(qū)大小
- IV ServerSocket 設(shè)置超時(shí)時(shí)間
- V ServerSocket 設(shè)置性能參數(shù)
I ServerSocket 端口號(hào)綁定參數(shù)
1. 建議綁定時(shí)機(jī) : 綁定端口號(hào)的操作建議在設(shè)置的最后一步進(jìn)行操作 , 如果綁定了端口號(hào) , 很多設(shè)置就無(wú)效了 ;
2. int backlog 參數(shù)作用 : 創(chuàng)建 ServerSocket 對(duì)象之后 , 需要綁定本地的 IP 地址和端口號(hào) , 服務(wù)器套接字綁定方法還有一個(gè) int backlog 參數(shù) , 這個(gè)參數(shù)指的是允許等待的連接隊(duì)列 , 如將該值設(shè)置成 10 的效果是 , 當(dāng)客戶端連接服務(wù)器 , 但是服務(wù)器還沒(méi)有調(diào)用 accept 方法接收客戶端的連接 , 此時(shí)如果有 10 個(gè)以內(nèi)的客戶端連接 , 這 10 個(gè)連接都在緩沖區(qū)中等待 , 如果出現(xiàn)第 11 個(gè)客戶端連接 , 此時(shí)客戶端就會(huì)拋出異常 ; 注意這個(gè)異常是在客戶端觸發(fā)的 ;
//服務(wù)器端綁定本地的 IP 地址和端口號(hào) serverSocket.bind(new InetSocketAddress(Inet4Address.getLocalHost(), 8888), 10);II ServerSocket 復(fù)用綁定端口設(shè)置
設(shè)置是否可以復(fù)用 ServerSocket 綁定的地址和端口號(hào) : setReuseAddress( true ) ;
serverSocket.setReuseAddress(true);Socket 連接在建立時(shí) , 會(huì)使用之前綁定本地的 IP 地址和端口號(hào) , 這個(gè)端口號(hào)在使用之后 , 2 分鐘之內(nèi)不允許再次使用 ; 進(jìn)行了該設(shè)置之后 , 可以在連接關(guān)閉之后 , 馬上使用該本地 IP 地址和端口號(hào) ;
III ServerSocket 設(shè)置緩沖區(qū)大小
1. 緩沖區(qū)大小設(shè)置 : ServerSocket 只有接收緩沖區(qū)設(shè)置 , 其原理與 Socket 緩沖區(qū)原理相同 ;
- ① 接收緩沖區(qū)設(shè)置 ;
2. 設(shè)置時(shí)機(jī) : 注意設(shè)置緩沖區(qū)一定要在 accept 之前進(jìn)行設(shè)置 , 如果在連接建立之后設(shè)置該緩沖區(qū)是無(wú)效的 ;
IV ServerSocket 設(shè)置超時(shí)時(shí)間
1. 設(shè)置 ServerSocket 超時(shí)時(shí)間 , 該超時(shí)時(shí)間沒(méi)有實(shí)際的概念 , 用于設(shè)置與阻塞相關(guān)操作的超時(shí)時(shí)間 , ServerSocket 中只有 accept 操作會(huì)有阻塞 , 設(shè)置了 2 秒阻塞時(shí)間 , 如果 accept 阻塞超過(guò) 2000 毫秒 , 就會(huì)拋出異常 , 此時(shí)可以捕獲該異常繼續(xù)等待 2 秒 ;
serverSocket.setSoTimeout(2000);一般情況下不設(shè)置該超時(shí)時(shí)間 , 即服務(wù)器端永久等待客戶端連接
2. 單位 : 毫秒 ( ms ) ;
V ServerSocket 設(shè)置性能參數(shù)
ServerSocket 調(diào)用 setPerformancePreferences 設(shè)置性能參數(shù) , 與 Socket 調(diào)用 setPerformancePreferences 設(shè)置原理是一樣的 ;
//對(duì)延遲性能要求最高 serverSocket.setPerformancePreferences(1, 10, 1);1. 調(diào)用 ServerSocket 對(duì)象的 setPerformancePreferences 方法 , 設(shè)置連接的性能參數(shù) ; 連接有以下三個(gè)性能參數(shù) :
- ① 連接時(shí)間 ;
- ② 往返延遲 ;
- ③ 帶寬 ;
2. 設(shè)置的是權(quán)重不是具體性能參數(shù) : 設(shè)置的值不是具體的參數(shù) , 而是連接的性能權(quán)重 , 對(duì)哪個(gè)性能要求比較高 ;
3. 連接時(shí)間 : 如果該 Socket 的連接很頻繁 , 連接后傳一個(gè)數(shù)據(jù) , 馬上斷開(kāi) , 這時(shí)候比較看重連接時(shí)間性能 , 此時(shí)可以將第一個(gè)參數(shù)設(shè)置成 10 , 后兩個(gè)參數(shù)設(shè)置成 1 , 表示注重連接時(shí)間性能 ;
//設(shè)置 連接時(shí)間 性能參數(shù)較重要 socket.setPerformancePreferences(10, 1, 1);4. 往返延遲 : 如果開(kāi)發(fā)的是網(wǎng)游服務(wù)器 , 此時(shí)對(duì)延遲很看重 , 這時(shí)候可以將第二個(gè)參數(shù)設(shè)置成比較高的權(quán)重 ;
//設(shè)置 往返延遲 性能參數(shù)較重要 socket.setPerformancePreferences(1, 10, 1);5. 帶寬 : 如果開(kāi)發(fā)的是音視頻服務(wù)器 , 注重帶寬性能 , 此時(shí)需要將第三個(gè)參數(shù)設(shè)置成較高的權(quán)重 ;
//設(shè)置 帶寬 性能參數(shù)較重要 socket.setPerformancePreferences(1, 10, 1);6. 上面的延遲和帶寬的性能是互斥的 , 延遲低 , 就意味著很小的包就要發(fā)送一次 , 其帶寬就低了 , 延遲高了 , 每次積累很多數(shù)據(jù)才發(fā)送 , 其帶寬就相應(yīng)的提高了 ;
7. 函數(shù)原型 :
/*** 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) 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的【Java 网络编程】服务器端 ServerSocket 配置 ( 端口复用 | 缓冲区设置 | 超时时间 | 性能权重 | 端口绑定 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Java 网络编程】客户端 Socke
- 下一篇: 【Java 网络编程】TCP 数据传输示