Java Code Examples for java.net.Socket#setOOBInline()
The following examples show how to use
java.net.Socket#setOOBInline() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UtilsTest.java From grpc-java with Apache License 2.0 | 6 votes |
@Test public void getSocketOptions() throws Exception { Socket socket = new Socket(); socket.setSoLinger(true, 2); socket.setSoTimeout(3); socket.setTcpNoDelay(true); socket.setReuseAddress(true); socket.setReceiveBufferSize(4000); socket.setSendBufferSize(5000); socket.setKeepAlive(true); socket.setOOBInline(true); socket.setTrafficClass(8); // note: see javadoc for valid input values SocketOptions socketOptions = Utils.getSocketOptions(socket); assertEquals(2, (int) socketOptions.lingerSeconds); assertEquals(3, (int) socketOptions.soTimeoutMillis); assertEquals("true", socketOptions.others.get("TCP_NODELAY")); assertEquals("true", socketOptions.others.get("SO_REUSEADDR")); assertEquals("4000", socketOptions.others.get("SO_RECVBUF")); assertEquals("5000", socketOptions.others.get("SO_SNDBUF")); assertEquals("true", socketOptions.others.get("SO_KEEPALIVE")); assertEquals("true", socketOptions.others.get("SO_OOBINLINE")); assertEquals("8", socketOptions.others.get("IP_TOS")); }
Example 2
Source File: UtilsTest.java From grpc-nebula-java with Apache License 2.0 | 6 votes |
@Test public void getSocketOptions() throws Exception { Socket socket = new Socket(); socket.setSoLinger(true, 2); socket.setSoTimeout(3); socket.setTcpNoDelay(true); socket.setReuseAddress(true); socket.setReceiveBufferSize(4000); socket.setSendBufferSize(5000); socket.setKeepAlive(true); socket.setOOBInline(true); socket.setTrafficClass(8); // note: see javadoc for valid input values SocketOptions socketOptions = Utils.getSocketOptions(socket); assertEquals(2, (int) socketOptions.lingerSeconds); assertEquals(3, (int) socketOptions.soTimeoutMillis); assertEquals("true", socketOptions.others.get("TCP_NODELAY")); assertEquals("true", socketOptions.others.get("SO_REUSEADDR")); assertEquals("4000", socketOptions.others.get("SO_RECVBUF")); assertEquals("5000", socketOptions.others.get("SO_SNDBUF")); assertEquals("true", socketOptions.others.get("SO_KEEPALIVE")); assertEquals("true", socketOptions.others.get("SO_OOBINLINE")); assertEquals("8", socketOptions.others.get("IP_TOS")); }
Example 3
Source File: SocketFactoryImpl.java From Quicksql with MIT License | 6 votes |
/** * Applies the current settings to the given socket. * * @param s Socket to apply the settings to * @return Socket the input socket */ protected Socket applySettings(Socket s) { try { s.setKeepAlive(SO_KEEPALIVE); s.setOOBInline(OOBINLINE); s.setReuseAddress(SO_REUSEADDR); s.setTcpNoDelay(TCP_NODELAY); s.setOOBInline(OOBINLINE); s.setReceiveBufferSize(SO_RCVBUF); s.setSendBufferSize(SO_SNDBUF); s.setSoTimeout(SO_TIMEOUT); s.setSoLinger(SO_LINGER, LINGER); } catch (SocketException e) { throw new RuntimeException(e); } return s; }
Example 4
Source File: SocketFactoryImpl.java From calcite with Apache License 2.0 | 6 votes |
/** * Applies the current settings to the given socket. * * @param s Socket to apply the settings to * @return Socket the input socket */ protected Socket applySettings(Socket s) { try { s.setKeepAlive(SO_KEEPALIVE); s.setOOBInline(OOBINLINE); s.setReuseAddress(SO_REUSEADDR); s.setTcpNoDelay(TCP_NODELAY); s.setOOBInline(OOBINLINE); s.setReceiveBufferSize(SO_RCVBUF); s.setSendBufferSize(SO_SNDBUF); s.setSoTimeout(SO_TIMEOUT); s.setSoLinger(SO_LINGER, LINGER); } catch (SocketException e) { throw new RuntimeException(e); } return s; }
Example 5
Source File: SocketProperties.java From tomcatsrc with Apache License 2.0 | 6 votes |
public void setProperties(Socket socket) throws SocketException{ if (rxBufSize != null) socket.setReceiveBufferSize(rxBufSize.intValue()); if (txBufSize != null) socket.setSendBufferSize(txBufSize.intValue()); if (ooBInline !=null) socket.setOOBInline(ooBInline.booleanValue()); if (soKeepAlive != null) socket.setKeepAlive(soKeepAlive.booleanValue()); if (performanceConnectionTime != null && performanceLatency != null && performanceBandwidth != null) socket.setPerformancePreferences( performanceConnectionTime.intValue(), performanceLatency.intValue(), performanceBandwidth.intValue()); if (soReuseAddress != null) socket.setReuseAddress(soReuseAddress.booleanValue()); if (soLingerOn != null && soLingerTime != null) socket.setSoLinger(soLingerOn.booleanValue(), soLingerTime.intValue()); if (soTimeout != null && soTimeout.intValue() >= 0) socket.setSoTimeout(soTimeout.intValue()); if (tcpNoDelay != null) socket.setTcpNoDelay(tcpNoDelay.booleanValue()); }
Example 6
Source File: SocketProperties.java From Tomcat7.0.67 with Apache License 2.0 | 6 votes |
public void setProperties(Socket socket) throws SocketException{ if (rxBufSize != null) socket.setReceiveBufferSize(rxBufSize.intValue()); if (txBufSize != null) socket.setSendBufferSize(txBufSize.intValue()); if (ooBInline !=null) socket.setOOBInline(ooBInline.booleanValue()); if (soKeepAlive != null) socket.setKeepAlive(soKeepAlive.booleanValue()); if (performanceConnectionTime != null && performanceLatency != null && performanceBandwidth != null) socket.setPerformancePreferences( performanceConnectionTime.intValue(), performanceLatency.intValue(), performanceBandwidth.intValue()); if (soReuseAddress != null) socket.setReuseAddress(soReuseAddress.booleanValue()); if (soLingerOn != null && soLingerTime != null) socket.setSoLinger(soLingerOn.booleanValue(), soLingerTime.intValue()); if (soTimeout != null && soTimeout.intValue() >= 0) socket.setSoTimeout(soTimeout.intValue()); if (tcpNoDelay != null) socket.setTcpNoDelay(tcpNoDelay.booleanValue()); }
Example 7
Source File: SocketOptions.java From logging-log4j2 with Apache License 2.0 | 5 votes |
public void apply(final Socket socket) throws SocketException { if (keepAlive != null) { socket.setKeepAlive(keepAlive.booleanValue()); } if (oobInline != null) { socket.setOOBInline(oobInline.booleanValue()); } if (reuseAddress != null) { socket.setReuseAddress(reuseAddress.booleanValue()); } if (performancePreferences != null) { performancePreferences.apply(socket); } if (receiveBufferSize != null) { socket.setReceiveBufferSize(receiveBufferSize.intValue()); } if (soLinger != null) { socket.setSoLinger(true, soLinger.intValue()); } if (soTimeout != null) { socket.setSoTimeout(soTimeout.intValue()); } if (tcpNoDelay != null) { socket.setTcpNoDelay(tcpNoDelay.booleanValue()); } final Integer actualTrafficClass = getActualTrafficClass(); if (actualTrafficClass != null) { socket.setTrafficClass(actualTrafficClass); } }
Example 8
Source File: BioReceiver.java From Tomcat8-Source-Read with MIT License | 5 votes |
public void listen() throws Exception { if (doListen()) { log.warn(sm.getString("bioReceiver.already.started")); return; } setListen(true); while ( doListen() ) { Socket socket = null; if ( getTaskPool().available() < 1 ) { if ( log.isWarnEnabled() ) log.warn(sm.getString("bioReceiver.threads.busy")); } BioReplicationTask task = (BioReplicationTask)getTaskPool().getRxTask(); if ( task == null ) continue; //should never happen try { socket = serverSocket.accept(); }catch ( Exception x ) { if ( doListen() ) throw x; } if ( !doListen() ) { task.setDoRun(false); task.serviceSocket(null,null); getExecutor().execute(task); break; //regular shutdown } if ( socket == null ) continue; socket.setReceiveBufferSize(getRxBufSize()); socket.setSendBufferSize(getTxBufSize()); socket.setTcpNoDelay(getTcpNoDelay()); socket.setKeepAlive(getSoKeepAlive()); socket.setOOBInline(getOoBInline()); socket.setReuseAddress(getSoReuseAddress()); socket.setSoLinger(getSoLingerOn(),getSoLingerTime()); socket.setSoTimeout(getTimeout()); ObjectReader reader = new ObjectReader(socket); task.serviceSocket(socket,reader); getExecutor().execute(task); }//while }
Example 9
Source File: SocketChannelTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testSocket_setOptions() throws IOException { channel1.connect(localAddr1); Socket socket = channel1.socket(); ByteBuffer buffer = ByteBuffer.wrap(new byte[] {1, 2, 3}); socket.setKeepAlive(true); channel1.write(buffer); socket.setOOBInline(true); channel1.write(buffer); socket.setReceiveBufferSize(100); channel1.write(buffer); socket.setReuseAddress(true); channel1.write(buffer); socket.setSendBufferSize(100); channel1.write(buffer); socket.setSoLinger(true, 100); channel1.write(buffer); socket.setSoTimeout(1000); channel1.write(buffer); socket.setTcpNoDelay(true); channel1.write(buffer); socket.setTrafficClass(10); channel1.write(buffer); }
Example 10
Source File: BioSender.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** * open real socket and set time out when waitForAck is enabled * is socket open return directly */ protected void openSocket() throws IOException { if(isConnected()) return ; try { socket = new Socket(); InetSocketAddress sockaddr = new InetSocketAddress(getAddress(), getPort()); socket.connect(sockaddr,(int)getTimeout()); socket.setSendBufferSize(getTxBufSize()); socket.setReceiveBufferSize(getRxBufSize()); socket.setSoTimeout( (int) getTimeout()); socket.setTcpNoDelay(getTcpNoDelay()); socket.setKeepAlive(getSoKeepAlive()); socket.setReuseAddress(getSoReuseAddress()); socket.setOOBInline(getOoBInline()); socket.setSoLinger(getSoLingerOn(),getSoLingerTime()); socket.setTrafficClass(getSoTrafficClass()); setConnected(true); soOut = socket.getOutputStream(); soIn = socket.getInputStream(); setRequestCount(0); setConnectTime(System.currentTimeMillis()); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.openSocket", getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0))); } catch (IOException ex1) { SenderState.getSenderState(getDestination()).setSuspect(); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.openSocket.failure",getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0)), ex1); throw (ex1); } }
Example 11
Source File: BioReceiver.java From tomcatsrc with Apache License 2.0 | 5 votes |
public void listen() throws Exception { if (doListen()) { log.warn("ServerSocket already started"); return; } setListen(true); while ( doListen() ) { Socket socket = null; if ( getTaskPool().available() < 1 ) { if ( log.isWarnEnabled() ) log.warn("All BIO server replication threads are busy, unable to handle more requests until a thread is freed up."); } BioReplicationTask task = (BioReplicationTask)getTaskPool().getRxTask(); if ( task == null ) continue; //should never happen try { socket = serverSocket.accept(); }catch ( Exception x ) { if ( doListen() ) throw x; } if ( !doListen() ) { task.setDoRun(false); task.serviceSocket(null,null); getExecutor().execute(task); break; //regular shutdown } if ( socket == null ) continue; socket.setReceiveBufferSize(getRxBufSize()); socket.setSendBufferSize(getTxBufSize()); socket.setTcpNoDelay(getTcpNoDelay()); socket.setKeepAlive(getSoKeepAlive()); socket.setOOBInline(getOoBInline()); socket.setReuseAddress(getSoReuseAddress()); socket.setSoLinger(getSoLingerOn(),getSoLingerTime()); socket.setSoTimeout(getTimeout()); ObjectReader reader = new ObjectReader(socket); task.serviceSocket(socket,reader); getExecutor().execute(task); }//while }
Example 12
Source File: BioSender.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
/** * open real socket and set time out when waitForAck is enabled * is socket open return directly */ protected void openSocket() throws IOException { if(isConnected()) return ; try { socket = new Socket(); InetSocketAddress sockaddr = new InetSocketAddress(getAddress(), getPort()); socket.connect(sockaddr,(int)getTimeout()); socket.setSendBufferSize(getTxBufSize()); socket.setReceiveBufferSize(getRxBufSize()); socket.setSoTimeout( (int) getTimeout()); socket.setTcpNoDelay(getTcpNoDelay()); socket.setKeepAlive(getSoKeepAlive()); socket.setReuseAddress(getSoReuseAddress()); socket.setOOBInline(getOoBInline()); socket.setSoLinger(getSoLingerOn(),getSoLingerTime()); socket.setTrafficClass(getSoTrafficClass()); setConnected(true); soOut = socket.getOutputStream(); soIn = socket.getInputStream(); setRequestCount(0); setConnectTime(System.currentTimeMillis()); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.openSocket", getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0))); } catch (IOException ex1) { SenderState.getSenderState(getDestination()).setSuspect(); if (log.isDebugEnabled()) log.debug(sm.getString("IDataSender.openSocket.failure",getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0)), ex1); throw (ex1); } }
Example 13
Source File: BioReceiver.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
public void listen() throws Exception { if (doListen()) { log.warn("ServerSocket already started"); return; } setListen(true); while ( doListen() ) { Socket socket = null; if ( getTaskPool().available() < 1 ) { if ( log.isWarnEnabled() ) log.warn("All BIO server replication threads are busy, unable to handle more requests until a thread is freed up."); } BioReplicationTask task = (BioReplicationTask)getTaskPool().getRxTask(); if ( task == null ) continue; //should never happen try { socket = serverSocket.accept(); }catch ( Exception x ) { if ( doListen() ) throw x; } if ( !doListen() ) { task.setDoRun(false); task.serviceSocket(null,null); getExecutor().execute(task); break; //regular shutdown } if ( socket == null ) continue; socket.setReceiveBufferSize(getRxBufSize()); socket.setSendBufferSize(getTxBufSize()); socket.setTcpNoDelay(getTcpNoDelay()); socket.setKeepAlive(getSoKeepAlive()); socket.setOOBInline(getOoBInline()); socket.setReuseAddress(getSoReuseAddress()); socket.setSoLinger(getSoLingerOn(),getSoLingerTime()); socket.setSoTimeout(getTimeout()); ObjectReader reader = new ObjectReader(socket); task.serviceSocket(socket,reader); getExecutor().execute(task); }//while }
Example 14
Source File: Config.java From mts with GNU General Public License v3.0 | 5 votes |
public static void getConfigForTCPSocket(Socket socket, boolean tlsTransport) throws Exception { boolean keepAlive = Config.getConfigByName("tcp.properties").getBoolean("socket.KEEP_ALIVE", false); socket.setKeepAlive(keepAlive); if (!tlsTransport) { boolean oobInline = Config.getConfigByName("tcp.properties").getBoolean("socket.OOB_INLINE", false); socket.setOOBInline(oobInline); } int receiveBuffer = Config.getConfigByName("tcp.properties").getInteger("socket.RECEIVE_BUFFER", 8192); socket.setReceiveBufferSize(receiveBuffer); boolean reuseAddress = Config.getConfigByName("tcp.properties").getBoolean("socket.REUSE_ADDRESS", false); socket.setReuseAddress(reuseAddress); int sendBuffer = Config.getConfigByName("tcp.properties").getInteger("socket.SEND_BUFFER", 8192); socket.setSendBufferSize(sendBuffer); int lingerTimeout = Config.getConfigByName("tcp.properties").getInteger("socket.LINGER_TIMEOUT", -1); if (lingerTimeout >= 0) socket.setSoLinger(true, lingerTimeout); else socket.setSoLinger(false, 0); int timeout = Config.getConfigByName("tcp.properties").getInteger("socket.TIMEOUT", 0); socket.setSoTimeout(timeout); boolean tcpNoDelay = Config.getConfigByName("tcp.properties").getBoolean("socket.TCP_NO_DELAY", false); socket.setTcpNoDelay(tcpNoDelay); int trafficClass = Config.getConfigByName("tcp.properties").getInteger("socket.TRAFFIC_CLASS", 0); socket.setTrafficClass(trafficClass); }
Example 15
Source File: SocketProperties.java From Tomcat8-Source-Read with MIT License | 5 votes |
public void setProperties(Socket socket) throws SocketException{ if (rxBufSize != null) socket.setReceiveBufferSize(rxBufSize.intValue()); if (txBufSize != null) socket.setSendBufferSize(txBufSize.intValue()); if (ooBInline !=null) socket.setOOBInline(ooBInline.booleanValue()); if (soKeepAlive != null) socket.setKeepAlive(soKeepAlive.booleanValue()); if (performanceConnectionTime != null && performanceLatency != null && performanceBandwidth != null) socket.setPerformancePreferences( performanceConnectionTime.intValue(), performanceLatency.intValue(), performanceBandwidth.intValue()); if (soReuseAddress != null) socket.setReuseAddress(soReuseAddress.booleanValue()); if (soLingerOn != null && soLingerTime != null) socket.setSoLinger(soLingerOn.booleanValue(), soLingerTime.intValue()); if (soTimeout != null && soTimeout.intValue() >= 0) socket.setSoTimeout(soTimeout.intValue()); if (tcpNoDelay != null) { try { socket.setTcpNoDelay(tcpNoDelay.booleanValue()); } catch (SocketException e) { // Some socket types may not support this option which is set by default } } }
Example 16
Source File: BioSender.java From Tomcat8-Source-Read with MIT License | 5 votes |
/** * Open real socket and set time out when waitForAck is enabled * is socket open return directly. * @throws IOException Error opening socket */ protected void openSocket() throws IOException { if(isConnected()) return ; try { socket = new Socket(); InetSocketAddress sockaddr = new InetSocketAddress(getAddress(), getPort()); socket.connect(sockaddr,(int)getTimeout()); socket.setSendBufferSize(getTxBufSize()); socket.setReceiveBufferSize(getRxBufSize()); socket.setSoTimeout( (int) getTimeout()); socket.setTcpNoDelay(getTcpNoDelay()); socket.setKeepAlive(getSoKeepAlive()); socket.setReuseAddress(getSoReuseAddress()); socket.setOOBInline(getOoBInline()); socket.setSoLinger(getSoLingerOn(),getSoLingerTime()); socket.setTrafficClass(getSoTrafficClass()); setConnected(true); soOut = socket.getOutputStream(); soIn = socket.getInputStream(); setRequestCount(0); setConnectTime(System.currentTimeMillis()); if (log.isDebugEnabled()) log.debug(sm.getString("bioSender.openSocket", getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0))); } catch (IOException ex1) { SenderState.getSenderState(getDestination()).setSuspect(); if (log.isDebugEnabled()) log.debug(sm.getString("bioSender.openSocket.failure",getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0)), ex1); throw ex1; } }
Example 17
Source File: HTTPUtil.java From nettythrift with Apache License 2.0 | 4 votes |
private static void configSocket(Socket socket) throws SocketException { /** * 设置Socket属性 */ // true表示关闭Socket的缓冲,立即发送数据..其默认值为false // 若Socket的底层实现不支持TCP_NODELAY选项,则会抛出SocketException socket.setTcpNoDelay(true); // 表示是否允许重用Socket所绑定的本地地址 socket.setReuseAddress(true); // 表示接收数据时的等待超时时间,单位毫秒..其默认值为0,表示会无限等待,永远不会超时 // 当通过Socket的输入流读数据时,如果还没有数据,就会等待 // 超时后会抛出SocketTimeoutException,且抛出该异常后Socket仍然是连接的,可以尝试再次读数据 socket.setSoTimeout(5000); // 表示当执行Socket.close()时,是否立即关闭底层的Socket // 这里设置为当Socket关闭后,底层Socket延迟5秒后再关闭,而5秒后所有未发送完的剩余数据也会被丢弃 // 默认情况下,执行Socket.close()方法,该方法会立即返回,但底层的Socket实际上并不立即关闭 // 它会延迟一段时间,直到发送完所有剩余的数据,才会真正关闭Socket,断开连接 // Tips:当程序通过输出流写数据时,仅仅表示程序向网络提交了一批数据,由网络负责输送到接收方 // Tips:当程序关闭Socket,有可能这批数据还在网络上传输,还未到达接收方 // Tips:这里所说的"未发送完的剩余数据"就是指这种还在网络上传输,未被接收方接收的数据 socket.setSoLinger(true, 5); // 表示发送数据的缓冲区的大小 socket.setSendBufferSize(1024); // 表示接收数据的缓冲区的大小 socket.setReceiveBufferSize(1024); // 表示对于长时间处于空闲状态(连接的两端没有互相传送数据)的Socket,是否要自动把它关闭,true为是 // 其默认值为false,表示TCP不会监视连接是否有效,不活动的客户端可能会永久存在下去,而不会注意到服务器已经崩溃 socket.setKeepAlive(true); // 表示是否支持发送一个字节的TCP紧急数据,socket.sendUrgentData(data)用于发送一个字节的TCP紧急数据 // 其默认为false,即接收方收到紧急数据时不作任何处理,直接将其丢弃..若用户希望发送紧急数据,则应设其为true // 设为true后,接收方会把收到的紧急数据与普通数据放在同样的队列中 socket.setOOBInline(true); // 该方法用于设置服务类型,以下代码请求高可靠性和最小延迟传输服务(把0x04与0x10进行位或运算) // Socket类用4个整数表示服务类型 // 0x02:低成本(二进制的倒数第二位为1) // 0x04:高可靠性(二进制的倒数第三位为1) // 0x08:最高吞吐量(二进制的倒数第四位为1) // 0x10:最小延迟(二进制的倒数第五位为1) socket.setTrafficClass(0x04 | 0x10); // 该方法用于设定连接时间,延迟,带宽的相对重要性(该方法的三个参数表示网络传输数据的3项指标) // connectionTime--该参数表示用最少时间建立连接 // latency---------该参数表示最小延迟 // bandwidth-------该参数表示最高带宽 // 可以为这些参数赋予任意整数值,这些整数之间的相对大小就决定了相应参数的相对重要性 // 如这里设置的就是---最高带宽最重要,其次是最小连接时间,最后是最小延迟 socket.setPerformancePreferences(2, 1, 3); }