设为首页 加入收藏

TOP

Java Socket 的参数选项解读(二)
2015-04-07 15:30:29 来源: 作者: 【 】 浏览:95
Tags:Java Socket 参数 选项 解读
.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.
? ? *
? ? * @param tc? ? ? ? an int value for the bitset.
? ? * @throws SocketException if there is an error setting the
? ? * traffic class or type-of-service
? ? * @since 1.4
? ? * @see #getTrafficClass
? ? */
? ? public void setTrafficClass(int tc) throws SocketException {
? ? 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, new Integer(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.
? ? *
? ? * @param on? ? whether or not to linger on.
? ? * @param linger how long to linger for, if on is true.
? ? * @exception SocketException if there is an error
? ? * in the underlying protocol, such as a TCP error.
? ? * @exception IllegalArgumentException if the linger value is negative.
? ? * @since JDK1.1
? ? * @see #getSoLinger()
? ? */
? ? public void setSoLinger(boolean on, int linger) throws SocketException {
? ? if (isClosed())
? ? ? ? throw new SocketException("Socket is closed");
? ? if (!on) {
? ? ? ? getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(on));
? ? } else {
? ? ? ? if (linger < 0) {
? ? ? ? throw new IllegalArgumentException("invalid value for SO_LINGER");
? ? ? ? }
? ? ? ? ? ? if (linger > 65535)
? ? ? ? ? ? ? ? linger = 65535;
? ? ? ? getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));
? ? }
? ? }


? ? ? 这个字段对Socket的close方法产生影响,当这个字段设置为false时,close会立即执行并返回,如果这时仍然有未被送出的数据包,那么这些数据包将被丢弃。如果设置为True时,有一个延迟时间可以设置。这个延迟时间就是close真正执行所有等待的时间,最大为65535。?


11、public final static int SO_TIMEOUT = 0x1006;?


/**
? ? *? Enable/disable SO_TIMEOUT with the speci

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java UUID 生成唯一标识 下一篇Linux C程序异常退出怎么办——co..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: