java setsolinger_java socket 的参数选项解读(转)
在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
java setsolinger_java socket 的参数选项解读(转)相关推荐
- java网络编程Socket中SO_LINGER选项的用法解读
http://blog.sina.com.cn/s/blog_6b1990eb0101171o.html 1:设置该选项: public void setSoLinger(boolean on, in ...
- Java socket 重要参数
Java Socket的api可能很多人会用,但是Java Socket的参数可能很多人都不知道用来干嘛的,甚至都不知道有这些参数. backlog 用于ServerSocket,配置ServerSo ...
- Java Socket重要参数讲解
Java Socket的api可能很多人会用,但是Java Socket的参数可能很多人都不知道用来干嘛的,甚至都不知道有这些参数. backlog 用于ServerSocket,配置ServerSo ...
- 【Java】Socket网络编程解读与实战
Socket原理 Socket原理 <Socket 到底是什么> Socket--套接字 是网络应用程序编程的接口和一种机制 用套接字中的相关函数来建立连接和完成通信 Socket可以看成 ...
- JAVA 通过 Socket 实现 TCP 编程
简介 TCP简介 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793定义.在简化的计算机 ...
- 【Android 组件化】路由组件 ( 注解处理器参数选项设置 )
文章目录 一.注解处理器 接收参数设置 二.注解处理器 生成路由表 Java 代码 三.博客资源 组件化系列博客 : [Android 组件化]从模块化到组件化 [Android 组件化]使用 Gra ...
- Java:socket服务端,socket服务端支持多连接,socket客户端,socket客户端支持发送和接受
一.Java之socket服务端 新建一个Java工程 命名 给他先创建一个类 在类里面我们做一个main 这里面也需要,创建套接字,IP号,端口号 但是java中有一个类 Serve ...
- Java Review - LinkedHashMap LinkedHashSet 源码解读
文章目录 Pre 概述 数据结构 类继承关系 构造函数 方法 get() put() remove() LinkedHashSet 使用案例 - FIFO策略缓存 Pre Java Review - ...
- Java 8 - Stream流骚操作解读2_归约操作
文章目录 Pre 什么是归约操作 元素求和 reduce reduce如何运行的 最大值和最小值 Pre Java 8 - Stream流骚操作解读见到过的终端操作都是返回一个 boolean ( a ...
最新文章
- webSocket浏览器握手不成功(解决)
- java 优先队列 用法_优先队列的基本用法(java和c++)
- 那些年,冒死拍过的老师逗逼搞笑照片 !
- 数据结构 - 最小堆最大堆
- 突发!Nginx 之父被拘留,原因竟然是“接私活儿”?
- linux shc shell脚本_使用shc工具加密shell脚本详解
- mysql做报表分析_mysqlreport解析
- VB6升级到VB2010之一:变量升级~
- AHK实现DD驱动按键连发
- Eclipse插件开发
- pdf阅读神器推荐——PDF-XChange Editor V8
- 手工玫瑰花_玫瑰手工
- 运维审计是什么意思?有什么作用?用什么软件好?
- office 2016 for Mac打开时 出现隐藏模块中出现编译错误: link
- 角摩电子书制作工具(JoymoEBook)v1.0绿色版
- “没有40K当什么程序员?”
- 明日召开 | Pulsar Summit Asia 2021 本周末线上精彩呈现
- 网络安全技术第六章——第三节木马的攻击与防治(中木马现象、木马病毒概念结构、木马实施攻击过程、配置传播运行连接木马、远程控制、木马伪装手段、更换图标改名换姓文件捆绑出错显示网页嫁衣自我销毁邮件附件)
- C语言文件操作实例代码
- ElasticSearch 从入门到入土
热门文章
- 栈计算机术语,计算机数据结构词汇中英对照
- mysql5.7环境,MySQL-5.7-线上生产环境部署
- matlab怎么整合成一个模块,Matlab如何将m文件制定成模块
- Spring boot的简单用法
- php js 图片旋转,jQuery实现可以控制图片旋转角度效果
- canvas 圆角矩形填充_一篇文章让你学会你最“害怕”的Canvas,太有意思了
- eps如何建立立体白模_服装立体裁剪教程 结构都是“立裁”出来的 才智服装
- 入门 IT 行业,该具备哪些技能?
- 学习关于display :flex 布局问题!
- ES6 解构赋值详解