在MulticastSocket的源代碼里有設置多播的方法:
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;
}
}
7、public final static int IP_MULTICAST_IF2 = 0x1f;
這個字段的效果和上面的是一樣的,只是擴展支持IPV6
8、public final static int IP_MULTICAST_LOOP = 0x12;
用來設置本地回環接口的多播特性,在MulticastSocket源代碼中有相關方法:
/*** 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));
}
9、public final static int IP_TOS = 0x3;
這個參數是用來控制IP頭中的TOS字段的,是用來控制和優化IP包的路徑的,在Socket源代碼里有一個設置的方法:
/*** 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));
}
從源代碼的注釋看,TOS設置了是否生效,和底層的操作系統的實現有關。應用程序無法保證TOS的變更會對socket連接產生影響。個人認為,TOS在一般情況下用不到。
10、public final static int SO_LINGER = 0x0080;
先看Socket源代碼:
/*** 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));
}
}
這個字段對Socket的close方法產生影響,當這個字段設置為false時,close會立即執行并返回,如果這時仍然有未被送出的數據包,那么這些數據包將被丟棄。如果設置為True時,有一個延遲時間可以設置。這個延遲時間就是close真正執行所有等待的時間,最大為65535。
11、public final static int SO_TIMEOUT = 0x1006;
/*** 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));
}
這個參數用來控制客戶端讀取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