java setsolinger_java socket 的參數選項解讀(轉)

在MulticastSocket的源代碼里有設置多播的方法:

48304ba5e6f9fe08f3fa1abda7d326ab.png

public void setInterface(InetAddress inf) throwsSocketException {

if(isClosed()) {

throw new SocketException("Socket is closed");

}

checkAddress(inf, "setInterface");

synchronized(infLock) {

getImpl().setOption(SocketOptions.IP_MULTICAST_IF, inf);

infAddress =inf;

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

7、public final static int IP_MULTICAST_IF2 = 0x1f;

這個字段的效果和上面的是一樣的,只是擴展支持IPV6

8、public final static int IP_MULTICAST_LOOP = 0x12;

用來設置本地回環接口的多播特性,在MulticastSocket源代碼中有相關方法:

48304ba5e6f9fe08f3fa1abda7d326ab.png

/*** Disable/Enable local loopback of multicast datagrams

* The option is used by the platform's networking code as a hint

* for setting whether multicast data will be looped back to

* the local socket.

*

*

Because this option is a hint, applications that want to

* verify what loopback mode is set to should call

* {@link#getLoopbackMode()}

* @paramdisable true to disable the LoopbackMode

* @throwsSocketException if an error occurs while setting the value

* @since1.4

* @see#getLoopbackMode

*/

public void setLoopbackMode(boolean disable) throwsSocketException {

getImpl().setOption(SocketOptions.IP_MULTICAST_LOOP, Boolean.valueOf(disable));

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

9、public final static int IP_TOS = 0x3;

這個參數是用來控制IP頭中的TOS字段的,是用來控制和優化IP包的路徑的,在Socket源代碼里有一個設置的方法:

48304ba5e6f9fe08f3fa1abda7d326ab.png

/*** Sets traffic class or type-of-service octet in the IP

* header for packets sent from this Socket.

* As the underlying network implementation may ignore this

* value applications should consider it a hint.

*

*

The tc must be in the range 0 <= tc <=

* 255 or an IllegalArgumentException will be thrown.

*

Notes:

*

For Internet Protocol v4 the value consists of an octet

* with precedence and TOS fields as detailed in RFC 1349. The

* TOS field is bitset created by bitwise-or'ing values such

* the following :-

*

*

*

IPTOS_LOWCOST (0x02)

*

IPTOS_RELIABILITY (0x04)

*

IPTOS_THROUGHPUT (0x08)

*

IPTOS_LOWDELAY (0x10)

*

* The last low order bit is always ignored as this

* corresponds to the MBZ (must be zero) bit.

*

* Setting bits in the precedence field may result in a

* SocketException indicating that the operation is not

* permitted.

*

* As RFC 1122 section 4.2.4.2 indicates, a compliant TCP

* implementation should, but is not required to, let application

* change the TOS field during the lifetime of a connection.

* So whether the type-of-service field can be changed after the

* TCP connection has been established depends on the implementation

* in the underlying platform. Applications should not assume that

* they can change the TOS field after the connection.

*

* For Internet Protocol v6 tc is the value that

* would be placed into the sin6_flowinfo field of the IP header.

*

* @paramtc an int value for the bitset.

* @throwsSocketException if there is an error setting the

* traffic class or type-of-service

* @since1.4

* @see#getTrafficClass

*/

public void setTrafficClass(int tc) throwsSocketException {

if (tc < 0 || tc > 255)

throw new IllegalArgumentException("tc is not in range 0 -- 255");

if(isClosed())

throw new SocketException("Socket is closed");

getImpl().setOption(SocketOptions.IP_TOS, newInteger(tc));

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

從源代碼的注釋看,TOS設置了是否生效,和底層的操作系統的實現有關。應用程序無法保證TOS的變更會對socket連接產生影響。個人認為,TOS在一般情況下用不到。

10、public final static int SO_LINGER = 0x0080;

先看Socket源代碼:

48304ba5e6f9fe08f3fa1abda7d326ab.png

/*** Enable/disable SO_LINGER with the specified linger time in seconds.

* The maximum timeout value is platform specific.

*

* The setting only affects socket close.

*

* @paramon whether or not to linger on.

* @paramlinger how long to linger for, if on is true.

* @exceptionSocketException if there is an error

* in the underlying protocol, such as a TCP error.

* @exceptionIllegalArgumentException if the linger value is negative.

* @sinceJDK1.1

* @see#getSoLinger()

*/

public void setSoLinger(boolean on, int linger) throwsSocketException {

if(isClosed())

throw new SocketException("Socket is closed");

if (!on) {

getImpl().setOption(SocketOptions.SO_LINGER, newBoolean(on));

} else{

if (linger < 0) {

throw new IllegalArgumentException("invalid value for SO_LINGER");

}

if (linger > 65535)

linger = 65535;

getImpl().setOption(SocketOptions.SO_LINGER, newInteger(linger));

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

這個字段對Socket的close方法產生影響,當這個字段設置為false時,close會立即執行并返回,如果這時仍然有未被送出的數據包,那么這些數據包將被丟棄。如果設置為True時,有一個延遲時間可以設置。這個延遲時間就是close真正執行所有等待的時間,最大為65535。

11、public final static int SO_TIMEOUT = 0x1006;

48304ba5e6f9fe08f3fa1abda7d326ab.png

/*** Enable/disable SO_TIMEOUT with the specified timeout, in

* milliseconds. With this option set to a non-zero timeout,

* a read() call on the InputStream associated with this Socket

* will block for only this amount of time. If the timeout expires,

* a java.net.SocketTimeoutException is raised, though the

* Socket is still valid. The option must be enabled

* prior to entering the blocking operation to have effect. The

* timeout must be > 0.

* A timeout of zero is interpreted as an infinite timeout.

* @paramtimeout the specified timeout, in milliseconds.

* @exceptionSocketException if there is an error

* in the underlying protocol, such as a TCP error.

* @sinceJDK 1.1

* @see#getSoTimeout()

*/

public synchronized void setSoTimeout(int timeout) throwsSocketException {

if(isClosed())

throw new SocketException("Socket is closed");

if (timeout < 0)

throw new IllegalArgumentException("timeout can't be negative");

getImpl().setOption(SocketOptions.SO_TIMEOUT, newInteger(timeout));

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

這個參數用來控制客戶端讀取socket數據的超時時間,如果timeout設置為0,那么就一直阻塞,否則阻塞直到超時后直接拋超時異常。

12、public final static int SO_SNDBUF = 0x1001;

在默認情況下,輸出流的發送緩沖區是8096個字節(8K)。這個值是Java所建議的輸出緩沖區的大小。如果這個默認值不能滿足要求,可以用setSendBufferSize方法來重新設置緩沖區的大小。

13、public final static int SO_RCVBUF = 0x1002;

在默認情況下,輸入流的接收緩沖區是8096個字節(8K)。這個值是Java所建議的輸入緩沖區的大小。如果這個默認值不能滿足要求,可以用setReceiveBufferSize方法來重新設置緩沖區的大小。

14、public final static int SO_KEEPALIVE = 0x0008;

如果將這個參數這是為True,客戶端每隔一段時間(一般不少于2小時)就像服務器發送一個試探性的數據包,服務器一般會有三種回應:

1、服務器正常回一個ACK,這表明遠程服務器一切OK,那么客戶端不會關閉連接,而是再下一個2小時后再發個試探包。

2、服務器返回一個RST,這表明遠程服務器掛了,這時候客戶端會關閉連接。

3、如果服務器未響應這個數據包,在大約11分鐘后,客戶端Socket再發送一個數據包,如果在12分鐘內,服務器還沒響應,那么客戶端Socket將關閉。

15、public final static int SO_OOBINLINE = 0x1003;

如果這個Socket選項打開,可以通過Socket類的sendUrgentData方法向服務器發送一個單字節的數據。這個單字節數據并不經過輸出緩沖區,而是立即發出。雖然在客戶端并不是使用OutputStream向服務器發送數據,但在服務端程序中這個單字節的數據是和其它的普通數據混在一起的。因此,在服務端程序中并不知道由客戶端發過來的數據是由OutputStream還是由sendUrgentData發過來的。

http://www.cnblogs.com/biakia/p/4321800.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/373415.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373415.shtml
英文地址,請注明出處:http://en.pswp.cn/news/373415.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【轉】Linux終端下 dstat 監控工具

轉自https://linux.cn/article-3215-1.html dstat 是一個可以取代vmstat&#xff0c;iostat&#xff0c;netstat和ifstat這些命令的多功能產品。dstat克服了這些命令的局限并增加了一些另外的功能&#xff0c;增加了監控項&#xff0c;也變得更靈活了。dstat可以很方便監控系統…

Tomcat和IntelliJ –在webapps文件夾之外部署war文件

目前&#xff0c;我正在開發一個Android應用程序&#xff0c;該應用程序需要云中托管的大量REST服務來支持。 我基于對Java&#xff0c;Groovy以及最重要的Spring的支持選擇了Google App Engine 。 我開發了一個基于Spring MVC的REST應用程序&#xff0c;并使用ContentNegotiat…

[HDU1232] 暢通工程 (并查集 or 連通分量)

Input 測試輸入包含若干測試用例。每個測試用例的第1行給出兩個正整數&#xff0c;分別是城鎮數目N ( < 1000 )和道路數目M&#xff1b;隨后的M行對應M條道路&#xff0c;每行給出一對正整數&#xff0c;分別是該條道路直接連通的兩個城鎮的編號。為簡單起見&#xff0c;城鎮…

java jdbc連接db2數據庫_Java連接db2數據庫(常用數據庫連接五)

1.安裝好db2數據庫&#xff0c;并建立表如下&#xff1a;2.eclipse或myeclipse中建立工程并導入java連接db2所需要的jar包db2java.jar 下載地址&#xff1a;http://download.csdn.net/detail/whzhaochao/64149813.建立iConn接口&#xff0c;代碼如下&#xff1a;package com.zh…

在Windows上,遷移VisualSVN server

最近在搭建自動化測試框架&#xff0c;順便了解了一下SVN的搭建。對于一般的使用場景&#xff0c;VisualSVN還是挺方便的&#xff0c;而且上手特別快。 由于是第一個demo&#xff0c;后期要遷移到其他服務器上面&#xff0c;所以就熟悉了一下server的遷移。以下是一些記錄信息&…

練習腳本三:日志清除

日志清除 #!/bin/bash #清除日志腳本&#xff0c;版本2 LOG_DIR/var/logROOT_UID0 #$UID為0的時候&#xff0c;用戶才具有root用戶的權限#判斷是否使用root用戶來運行 if [ "$UID" -ne "$ROOT_UID" ];thenecho "Must be root to run this script.&qu…

Oracle通過邀請Weaver和Chin推動JavaFX向前發展

我昨天發布了愚人節帖子&#xff0c;內容涉及加入NASA協助探索紅色大行星。 那個帖子與事實相距不遠... NASA開發的技術的所有細節都是100&#xff05;準確的。 哎呀&#xff0c;即使我辭職也是事實&#xff01; 唯一不正確的部分是我將加入的公司。 在NASA協助探索火星的工作也…

java privilege的用法_java反射--注解的定義與運用以及權限攔截

自定義注解類編寫的一些規則:1. Annotation型定義為interface, 所有的Annotation會自動繼承java.lang.Annotation這一接口,并且不能再去繼承別的類或是接口.2. 參數成員只能用public或默認(default)這兩個訪問權修飾3. 參數成員只能用基本類型byte,short,char,int,long,float,d…

WinForm------TextEdit只能輸入數字

代碼: this.textEdit1.Properties.Mask.EditMask "\d"; this.textEdit1.Properties.Mask.MaskType MaskType.RegEx; 轉載于:https://www.cnblogs.com/tianhengblogs/p/6093634.html

mysql使用隨筆

mysql 刪除語句 &#xff1a;delete from 表名 where 條件; 例如 delete from tbuserinfo where id 2;mysql 查詢語句 &#xff1a;select * 列名 from 表名 where 條件;mysql 模糊查詢 &#xff1a; SELECT * FROM 表名 WHERE 列名 LIKE "3%&qu…

JavaFX:創建Sprite動畫

到目前為止&#xff0c;盡管我的大多數文章都涉及JavaFX屬性和綁定&#xff0c;但今天我想寫一講我也致力于JavaFX運行時的另一部分&#xff1a;動畫API。 在本文中&#xff0c;我將解釋如何在JavaFX中編寫自定義動畫&#xff0c;以及如何使用這種方法為Sprite動畫創建類。 &am…

java tick_Java中的Clock tick()方法

可以使用tick()Java中Clock類中的方法在所需的時間范圍內舍入基本時鐘的瞬間。此方法需要兩個參數&#xff0c;即基本時鐘和滴答的持續時間。同樣&#xff0c;返回在所需持續時間內四舍五入的基本時鐘時刻。演示此的程序如下所示-示例import java.time.*;public class Main {pu…

JAVA 常用框架和工具

集成開發工具&#xff08;IDE&#xff09;&#xff1a;Eclipse、MyEclipse、Spring Tool Suite&#xff08;STS&#xff09;、Intellij IDEA、NetBeans、JBuilder、JCreator JAVA服務器&#xff1a;tomcat、jboss、websphere、weblogic、resin、jetty、apusic、apache 負載均衡…

MySQL Doublewrite Buffer及業務評估

1. 關于Doublewrite Buffe的總結 Doublewrite Buffer&#xff1a;Doublewrite Buffer出現的初衷是防止buffer pool中的臟頁刷新到磁盤中&#xff0c;出現部分寫的問題&#xff0c;innodb頁大小一般為16k&#xff0c;而Linux操作系統的block size一般為4k。這樣在刷新的過程中&a…

使用UIBinder的GWT自定義按鈕

這是一個有關如何在GWT上使用UIBinder創建自定義按鈕的示例。 public class GwtUIBinderButton implements EntryPoint {public void onModuleLoad() {Button button new Button();button.setText("Button");button.addClickHandler(new ClickHandler(){Overridepub…

delete postman 傳參_PostMan 傳參boolean 類型,接口接受的值一直是false

情形&#xff1a;最近寫前臺頁面的一個按鈕&#xff0c;功能是&#xff1a;點擊后切換狀態&#xff0c;顯示是或否。字段名稱是isTest,類型是boolean 。寫完接口&#xff0c;拿postMan測試&#xff0c;傳參如下&#xff1a;但是后臺接口接受的數據 一直是false,處理&#xff1a…

前端學PHP之文件操作

前端學PHP之文件操作 前面的話 在程序運行時&#xff0c;程序本身和數據一般都存在內存中&#xff0c;當程序運行結束后&#xff0c;存放在內存中的數據被釋放。如果需要長期保存程序運行所需的原始數據&#xff0c;或程序運行產生的結果&#xff0c;就需要把數據存儲在文件或數…

騰訊云CentOS6.5下安裝mysql,并配置好遠程訪問等權限,途中遇到的問題

1.使用yum命令安裝mysql [rootbogon ~]# yum -y install mysql-server 2.設置開機啟動 [rootbogon ~]# chkconfig mysqld on 3.啟動MySQL服務 [rootbogon ~]# service mysqld start 4.設置MySQL的root用戶設置密碼 [rootbogon ~]# mysql -u root mysql> select u…

休眠性能提示:臟收集效果

在使用Hibernate作為ORM開發服務器和嵌入式應用程序8年后&#xff0c;我全力以赴地尋求提高Hibernate性能的解決方案&#xff0c;閱讀博客和參加會議&#xff0c;我決定與您分享這幾年獲得的知識。 這是更多新帖子中的第一篇&#xff1a; 去年&#xff0c;我以Devoxx的身份參加…

java runtime 異常_Java中RuntimeException和Exception

在java的異常類體系中,Error和RuntimeException是非檢查型異常&#xff0c;其他的都是檢查型異常。所有方法都可以在不聲明throws的情況下拋出RuntimeException及其子類不可以在不聲明的情況下拋出非RuntimeException簡單的說&#xff0c;非RuntimeException必要自己寫catch塊處…