设为首页 加入收藏

TOP

Java Socket 的参数选项解读(三)
2015-04-07 15:30:29 来源: 作者: 【 】 浏览:94
Tags:Java Socket 参数 选项 解读
fied 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.
? ? * @param timeout the specified timeout, in milliseconds.
? ? * @exception SocketException if there is an error
? ? * in the underlying protocol, such as a TCP error.
? ? * @since? JDK 1.1
? ? * @see #getSoTimeout()
? ? */
? ? public synchronized void setSoTimeout(int timeout) throws SocketException {
? ? if (isClosed())
? ? ? ? throw new SocketException("Socket is closed");
? ? if (timeout < 0)
? ? ? throw new IllegalArgumentException("timeout can't be negative");


? ? getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(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发过来的。


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

评论

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