Java Code Examples for java.net.Socket#setTrafficClass()
The following examples show how to use
java.net.Socket#setTrafficClass() .
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-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 2
Source File: AdbConnection.java From SoloPi with Apache License 2.0 | 6 votes |
/** * Creates a AdbConnection object associated with the socket and * crypto object specified. * @param socket The socket that the connection will use for communcation. * @param crypto The crypto object that stores the key pair for authentication. * @return A new AdbConnection object. * @throws IOException If there is a socket error */ public static AdbConnection create(Socket socket, AdbCrypto crypto) throws IOException { AdbConnection newConn = new AdbConnection(); newConn.crypto = crypto; newConn.socket = socket; // 试试bufferedStream newConn.inputStream = socket.getInputStream(); newConn.outputStream = socket.getOutputStream(); /* Disable Nagle because we're sending tiny packets */ socket.setTcpNoDelay(true); socket.setTrafficClass(0x10); socket.setPerformancePreferences(1, 3, 2); return newConn; }
Example 3
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 4
Source File: StandardSocketFactory.java From FoxTelem with GNU General Public License v3.0 | 6 votes |
/** * Configures socket properties based on properties from the connection * (tcpNoDelay, snd/rcv buf, traffic class, etc). * * @param sock * socket * @param pset * properties * @throws SocketException * if an error occurs * @throws IOException * if an error occurs */ private void configureSocket(Socket sock, PropertySet pset) throws SocketException, IOException { sock.setTcpNoDelay(pset.getBooleanProperty(PropertyKey.tcpNoDelay).getValue()); sock.setKeepAlive(pset.getBooleanProperty(PropertyKey.tcpKeepAlive).getValue()); int receiveBufferSize = pset.getIntegerProperty(PropertyKey.tcpRcvBuf).getValue(); if (receiveBufferSize > 0) { sock.setReceiveBufferSize(receiveBufferSize); } int sendBufferSize = pset.getIntegerProperty(PropertyKey.tcpSndBuf).getValue(); if (sendBufferSize > 0) { sock.setSendBufferSize(sendBufferSize); } int trafficClass = pset.getIntegerProperty(PropertyKey.tcpTrafficClass).getValue(); if (trafficClass > 0) { sock.setTrafficClass(trafficClass); } }
Example 5
Source File: NioLaserServer.java From laser with GNU General Public License v3.0 | 5 votes |
/** * 配置SocketChannel * * @throws IOException */ private void configSocketChannel(SocketChannel socketChannel) throws IOException { socketChannel.configureBlocking(false); // config the socket final Socket socket = socketChannel.socket(); socket.setTcpNoDelay(options.isServerChildTcpNoDelay()); socket.setReceiveBufferSize(options.getServerChildSocketReceiverBufferSize()); socket.setSendBufferSize(options.getServerChildSocketSendBufferSize()); socket.setSoTimeout(options.getServerChildSocketTimeout()); socket.setPerformancePreferences( options.getServerChildPerformancePreferences()[0], options.getServerChildPerformancePreferences()[1], options.getServerChildPerformancePreferences()[2]); socket.setTrafficClass(options.getServerChildTrafficClass()); }
Example 6
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 7
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 8
Source File: WiFiP2PSocket.java From nfcspy with GNU General Public License v3.0 | 5 votes |
private static void tuneupSocket(Socket skt) throws SocketException { skt.setTcpNoDelay(true); skt.setTrafficClass(0x04 | 0x10); if (skt.getSendBufferSize() < WiFiP2PSocket.BUF_SIZE) skt.setSendBufferSize(WiFiP2PSocket.BUF_SIZE); if (skt.getReceiveBufferSize() < WiFiP2PSocket.BUF_SIZE) skt.setReceiveBufferSize(WiFiP2PSocket.BUF_SIZE); }
Example 9
Source File: Connection.java From drftpd with GNU General Public License v2.0 | 5 votes |
protected void setSockOpts(Socket sock) throws IOException { /* * IPTOS_LOWCOST (0x02) IPTOS_RELIABILITY (0x04) IPTOS_THROUGHPUT (0x08) * IPTOS_LOWDELAY (0x10) */ sock.setTrafficClass(0x08); sock.setSoTimeout(TIMEOUT); }
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: 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 12
Source File: Connection.java From drftpd with GNU General Public License v2.0 | 5 votes |
protected void setSockOpts(Socket sock) throws IOException { /* * IPTOS_LOWCOST (0x02) IPTOS_RELIABILITY (0x04) IPTOS_THROUGHPUT (0x08) * IPTOS_LOWDELAY (0x10) */ sock.setTrafficClass(0x08); sock.setSoTimeout(TIMEOUT); }
Example 13
Source File: StandardSocketFactory.java From Komondor with GNU General Public License v3.0 | 5 votes |
/** * Configures socket properties based on properties from the connection * (tcpNoDelay, snd/rcv buf, traffic class, etc). * * @param props * @throws SocketException * @throws IOException */ private void configureSocket(Socket sock, Properties props) throws SocketException, IOException { sock.setTcpNoDelay(Boolean.valueOf(props.getProperty(TCP_NO_DELAY_PROPERTY_NAME, TCP_NO_DELAY_DEFAULT_VALUE)).booleanValue()); String keepAlive = props.getProperty(TCP_KEEP_ALIVE_PROPERTY_NAME, TCP_KEEP_ALIVE_DEFAULT_VALUE); if (keepAlive != null && keepAlive.length() > 0) { sock.setKeepAlive(Boolean.valueOf(keepAlive).booleanValue()); } int receiveBufferSize = Integer.parseInt(props.getProperty(TCP_RCV_BUF_PROPERTY_NAME, TCP_RCV_BUF_DEFAULT_VALUE)); if (receiveBufferSize > 0) { sock.setReceiveBufferSize(receiveBufferSize); } int sendBufferSize = Integer.parseInt(props.getProperty(TCP_SND_BUF_PROPERTY_NAME, TCP_SND_BUF_DEFAULT_VALUE)); if (sendBufferSize > 0) { sock.setSendBufferSize(sendBufferSize); } int trafficClass = Integer.parseInt(props.getProperty(TCP_TRAFFIC_CLASS_PROPERTY_NAME, TCP_TRAFFIC_CLASS_DEFAULT_VALUE)); if (trafficClass > 0) { sock.setTrafficClass(trafficClass); } }
Example 14
Source File: StandardSocketFactory.java From r-course with MIT License | 5 votes |
/** * Configures socket properties based on properties from the connection * (tcpNoDelay, snd/rcv buf, traffic class, etc). * * @param props * @throws SocketException * @throws IOException */ private void configureSocket(Socket sock, Properties props) throws SocketException, IOException { sock.setTcpNoDelay(Boolean.valueOf(props.getProperty(TCP_NO_DELAY_PROPERTY_NAME, TCP_NO_DELAY_DEFAULT_VALUE)).booleanValue()); String keepAlive = props.getProperty(TCP_KEEP_ALIVE_PROPERTY_NAME, TCP_KEEP_ALIVE_DEFAULT_VALUE); if (keepAlive != null && keepAlive.length() > 0) { sock.setKeepAlive(Boolean.valueOf(keepAlive).booleanValue()); } int receiveBufferSize = Integer.parseInt(props.getProperty(TCP_RCV_BUF_PROPERTY_NAME, TCP_RCV_BUF_DEFAULT_VALUE)); if (receiveBufferSize > 0) { sock.setReceiveBufferSize(receiveBufferSize); } int sendBufferSize = Integer.parseInt(props.getProperty(TCP_SND_BUF_PROPERTY_NAME, TCP_SND_BUF_DEFAULT_VALUE)); if (sendBufferSize > 0) { sock.setSendBufferSize(sendBufferSize); } int trafficClass = Integer.parseInt(props.getProperty(TCP_TRAFFIC_CLASS_PROPERTY_NAME, TCP_TRAFFIC_CLASS_DEFAULT_VALUE)); if (trafficClass > 0) { sock.setTrafficClass(trafficClass); } }
Example 15
Source File: StandardSocketFactory.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Configures socket properties based on properties from the connection * (tcpNoDelay, snd/rcv buf, traffic class, etc). * * @param sock * socket * @param props * properties * @throws SocketException * if an error occurs * @throws IOException * if an error occurs */ private void configureSocket(Socket sock, Properties props) throws SocketException, IOException { sock.setTcpNoDelay(Boolean.valueOf(props.getProperty(PropertyDefinitions.PNAME_tcpNoDelay, TCP_NO_DELAY_DEFAULT_VALUE)).booleanValue()); String keepAlive = props.getProperty(PropertyDefinitions.PNAME_tcpKeepAlive, TCP_KEEP_ALIVE_DEFAULT_VALUE); if (keepAlive != null && keepAlive.length() > 0) { sock.setKeepAlive(Boolean.valueOf(keepAlive).booleanValue()); } int receiveBufferSize = Integer.parseInt(props.getProperty(PropertyDefinitions.PNAME_tcpRcvBuf, TCP_RCV_BUF_DEFAULT_VALUE)); if (receiveBufferSize > 0) { sock.setReceiveBufferSize(receiveBufferSize); } int sendBufferSize = Integer.parseInt(props.getProperty(PropertyDefinitions.PNAME_tcpSndBuf, TCP_SND_BUF_DEFAULT_VALUE)); if (sendBufferSize > 0) { sock.setSendBufferSize(sendBufferSize); } int trafficClass = Integer.parseInt(props.getProperty(PropertyDefinitions.PNAME_tcpTrafficClass, TCP_TRAFFIC_CLASS_DEFAULT_VALUE)); if (trafficClass > 0) { sock.setTrafficClass(trafficClass); } }
Example 16
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 17
Source File: Utils.java From TarsJava with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static void setQosFlag(Socket socket) { if (NETWORK_QOS_FLAG == null || socket == null) return; try { if ("B1_REAL_TIME_QOS".equals(NETWORK_QOS_FLAG)) { socket.setTrafficClass(144); } else if ("B1_NON_REAL_TIME_QOS".equals(NETWORK_QOS_FLAG)) { socket.setTrafficClass(24); } } catch (Exception ex) { ex.printStackTrace(); } }
Example 18
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); }
Example 19
Source File: SocketTest.java From j2objc with Apache License 2.0 | 4 votes |
public void test_setTrafficClass() throws Exception { Socket s = new Socket(); s.setTrafficClass(123); assertEquals(123, s.getTrafficClass()); }
Example 20
Source File: IncomingSocketChannelManager.java From BiglyBT with GNU General Public License v2.0 | 2 votes |
protected void process( int local_port, TransportHelperFilter filter ) { SocketChannel channel = ((TCPTransportHelper)filter.getHelper()).getSocketChannel(); Socket socket = channel.socket(); //set advanced socket options try { int so_sndbuf_size = COConfigurationManager.getIntParameter( "network.tcp.socket.SO_SNDBUF" ); if( so_sndbuf_size > 0 ) socket.setSendBufferSize( so_sndbuf_size ); String ip_tos = COConfigurationManager.getStringParameter( "network.tcp.socket.IPDiffServ" ); if( ip_tos.length() > 0 ) socket.setTrafficClass( Integer.decode( ip_tos ).intValue() ); } catch( Throwable t ) { t.printStackTrace(); } AEProxyAddressMapper.AppliedPortMapping applied_mapping = proxy_address_mapper.applyPortMapping( socket.getInetAddress(), socket.getPort()); InetSocketAddress tcp_address = applied_mapping.getRemoteAddress(); ConnectionEndpoint co_ep = new ConnectionEndpoint(tcp_address); Map<String,Object> properties = applied_mapping.getProperties(); if ( properties != null ){ co_ep.addProperties( properties ); } ProtocolEndpointTCP pe_tcp = (ProtocolEndpointTCP)ProtocolEndpointFactory.createEndpoint( ProtocolEndpoint.PROTOCOL_TCP, co_ep, tcp_address ); Transport transport = new TCPTransportImpl( pe_tcp, filter ); incoming_manager.addConnection( local_port, filter, transport ); }