当前位置: 首页>>代码示例>>Java>>正文


Java Socket.setTcpNoDelay方法代码示例

本文整理汇总了Java中java.net.Socket.setTcpNoDelay方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.setTcpNoDelay方法的具体用法?Java Socket.setTcpNoDelay怎么用?Java Socket.setTcpNoDelay使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.Socket的用法示例。


在下文中一共展示了Socket.setTcpNoDelay方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: openChannel

import java.net.Socket; //导入方法依赖的package包/类
public static Channel openChannel ( InetSocketAddress isa ) throws IOException, SocketException {
    System.err.println("* Opening socket " + isa);
    Socket s = SocketFactory.getDefault().createSocket(isa.getAddress(), isa.getPort());
    s.setKeepAlive(true);
    s.setTcpNoDelay(true);

    System.err.println("* Opening channel");
    OutputStream outputStream = s.getOutputStream();
    DataOutputStream dos = new DataOutputStream(outputStream);
    dos.writeUTF("Protocol:CLI-connect");
    ExecutorService cp = Executors.newCachedThreadPool(new ThreadFactory() {

        public Thread newThread ( Runnable r ) {
            Thread t = new Thread(r, "Channel");
            t.setDaemon(true);
            return t;
        }
    });
    Channel c = new ChannelBuilder("EXPLOIT", cp).withMode(Mode.BINARY).build(s.getInputStream(), outputStream);
    System.err.println("* Channel open");
    return c;
}
 
开发者ID:hucheat,项目名称:APacheSynapseSimplePOC,代码行数:23,代码来源:JenkinsCLI.java

示例2: connect

import java.net.Socket; //导入方法依赖的package包/类
public void connect() {

		if (!isConnected()) {

			try {
				socket = new Socket();
				socket.setReuseAddress(true);
				socket.setKeepAlive(true);
				socket.setTcpNoDelay(true);
				socket.setSoLinger(true, 0);
				
				socket.connect(new InetSocketAddress(host, port), connectionTimeout);
				if ( soTimeout > 0)
		          socket.setSoTimeout(soTimeout);
		        
		        outputStream = new RedisOutputStream(socket.getOutputStream());
		        inputStream = new RedisInputStream(socket.getInputStream());
		        
			} catch (IOException ex) {
				broken = true;
				throw new JedisConnectionException("Failed connecting to host " + host + ":" + port, ex);
			}
		}
	}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:25,代码来源:JedisConnection.java

示例3: b

import java.net.Socket; //导入方法依赖的package包/类
private static boolean b(String str) {
    long currentTimeMillis = System.currentTimeMillis();
    try {
        b.a("ConnectivityTest: begin to connect to " + str);
        Socket socket = new Socket();
        socket.connect(Host.b(str, 5222), BaseImageDownloader.DEFAULT_HTTP_CONNECT_TIMEOUT);
        socket.setTcpNoDelay(true);
        b.a("ConnectivityTest: connect to " + str + " in " + (System.currentTimeMillis() -
                currentTimeMillis));
        socket.close();
        return true;
    } catch (Throwable th) {
        b.d("ConnectivityTest: could not connect to:" + str + " exception: " + th.getClass()
                .getSimpleName() + " description: " + th.getMessage());
        return false;
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:18,代码来源:u.java

示例4: send

import java.net.Socket; //导入方法依赖的package包/类
private synchronized void send(Message message) throws IOException {
	if(message == null) return;
			
	Socket socket = new Socket();
	socket.setReuseAddress(true);
	socket.setPerformancePreferences(2, 1, 0); // connection, latency, bandwidth
	socket.setTcpNoDelay(true);
	socket.setTrafficClass(0x10);	// low delay
	socket.setSoTimeout(timeoutMillis);
	socket.connect(address);
		
	@SuppressWarnings("resource")
       OutputStream out = socket.getOutputStream();
	
	// TODO additional check. Is it needed?
	if(out == null) throw new IOException("Socket has no output stream.");
	
	String text = message.toString();
	out.write(text.getBytes());
	
	if(CRUSH.debug) CRUSH.debug(this, "DRP> " + text);
	
	out.flush();
	socket.close();
}
 
开发者ID:attipaci,项目名称:crush,代码行数:26,代码来源:DRPMessenger.java

示例5: bind

import java.net.Socket; //导入方法依赖的package包/类
@Override
public void bind(
        final Socket socket,
        final HttpParams params) throws IOException {
    Args.notNull(socket, "Socket");
    Args.notNull(params, "HTTP parameters");
    assertNotOpen();
    socket.setTcpNoDelay(params.getBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true));
    socket.setSoTimeout(params.getIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0));
    socket.setKeepAlive(params.getBooleanParameter(CoreConnectionPNames.SO_KEEPALIVE, false));
    final int linger = params.getIntParameter(CoreConnectionPNames.SO_LINGER, -1);
    if (linger >= 0) {
        socket.setSoLinger(linger > 0, linger);
    }
    super.bind(socket, params);
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:17,代码来源:DefaultHttpClientConnection.java

示例6: createWireIO

import java.net.Socket; //导入方法依赖的package包/类
public WireIO createWireIO(Socket clientSocket) {
    try {
        clientSocket.setTcpNoDelay(true); // Necessary at least on Solaris to avoid delays in e.g. readInt() etc.

        ObjectInputStream socketIn = new ObjectInputStream(clientSocket.getInputStream());
        ObjectOutputStream socketOut = new ObjectOutputStream(clientSocket.getOutputStream());
        WireIO wireIO = new WireIO(socketOut, socketIn);

        return wireIO;
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:16,代码来源:CommonWireIOTestCase.java

示例7: peerFromSocket

import java.net.Socket; //导入方法依赖的package包/类
public static Peer peerFromSocket(Socket socket)
    throws IOException {
  Peer peer = null;
  boolean success = false;
  try {
    // TCP_NODELAY is crucial here because of bad interactions between
    // Nagle's Algorithm and Delayed ACKs. With connection keepalive
    // between the client and DN, the conversation looks like:
    //   1. Client -> DN: Read block X
    //   2. DN -> Client: data for block X
    //   3. Client -> DN: Status OK (successful read)
    //   4. Client -> DN: Read block Y
    // The fact that step #3 and #4 are both in the client->DN direction
    // triggers Nagling. If the DN is using delayed ACKs, this results
    // in a delay of 40ms or more.
    //
    // TCP_NODELAY disables nagling and thus avoids this performance
    // disaster.
    socket.setTcpNoDelay(true);
    SocketChannel channel = socket.getChannel();
    if (channel == null) {
      peer = new BasicInetPeer(socket);
    } else {
      peer = new NioInetPeer(socket);
    }
    success = true;
    return peer;
  } finally {
    if (!success) {
      if (peer != null) peer.close();
      socket.close();
    }
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:35,代码来源:TcpPeerServer.java

示例8: connect

import java.net.Socket; //导入方法依赖的package包/类
public void connect() throws UnknownHostException, IOException {
	LOG.log(Level.INFO, "connecting to " + host + ":" +  port);
	socket = new Socket(host, port);
	socket.setTcpNoDelay(true);
	socket.setSoTimeout(DEFAULT_TIMEOUT);
	in = socket.getInputStream();
	writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
	thread = new URClientThread(name);
	thread.start();
	connected = true;
}
 
开发者ID:tudrobotics,项目名称:urdriver,代码行数:12,代码来源:URClient.java

示例9: GUIControlChannel

import java.net.Socket; //导入方法依赖的package包/类
/**
 * Try to connect to a remote FDT instance
 *
 * @param inetAddress
 * @param port
 * @param notifier
 * @throws Exception
 */
public GUIControlChannel(InetAddress inetAddress, int port, GUIControlChannelNotifier notifier) throws Exception {
    try {
        this.notifier = notifier;

        controlSocket = new Socket();
        controlSocket.connect(new InetSocketAddress(inetAddress, port), CONNECT_TIMEOUT);

        this.remoteAddress = inetAddress;
        this.remotePort = port;
        this.localPort = controlSocket.getLocalPort();

        controlSocket.setTcpNoDelay(true);

        //only the first octet will be interpreted by the AcceptTask at the other end
        controlSocket.getOutputStream().write(new byte[]{3});

        //from now on only CtrlMsg will be sent
        initStreams();
        controlSocket.setSoTimeout(1000);

        //
    } catch (Throwable t) {
        close("Cannot instantiate ControlChannel", t);
        throw new Exception(t);
    }
}
 
开发者ID:fast-data-transfer,项目名称:fdt,代码行数:35,代码来源:GUIControlChannel.java

示例10: setAcceptedSocketOptions

import java.net.Socket; //导入方法依赖的package包/类
public void setAcceptedSocketOptions(Acceptor acceptor,
                                     ServerSocket serverSocket,
                                     Socket socket)
    throws SocketException
{
    // Disable Nagle's algorithm (i.e., always send immediately).
    socket.setTcpNoDelay(true);
    if (keepAlive)
        socket.setKeepAlive(true);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:DefaultSocketFactoryImpl.java

示例11: setSocketOptions

import java.net.Socket; //导入方法依赖的package包/类
/**
 * Set the options for the current socket.
 */
protected boolean setSocketOptions(Socket socket) {
    // Process the connection
    int step = 1;
    try {

        // 1: Set socket options: timeout, linger, etc
        if (soLinger >= 0) { 
            socket.setSoLinger(true, soLinger);
        }
        if (tcpNoDelay) {
            socket.setTcpNoDelay(tcpNoDelay);
        }
        if (soTimeout > 0) {
            socket.setSoTimeout(soTimeout);
        }

        // 2: SSL handshake
        step = 2;
        serverSocketFactory.handshake(socket);

    } catch (Throwable t) {
        if (log.isDebugEnabled()) {
            if (step == 2) {
                log.debug(sm.getString("endpoint.err.handshake"), t);
            } else {
                log.debug(sm.getString("endpoint.err.unexpected"), t);
            }
        }
        // Tell to close the socket
        return false;
    }
    return true;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:37,代码来源:JIoEndpoint.java

示例12: openSocket

import java.net.Socket; //导入方法依赖的package包/类
/**
 * 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);
    }
    
 }
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:35,代码来源:BioSender.java

示例13: prepareSocket

import java.net.Socket; //导入方法依赖的package包/类
/**
 * Performs standard initializations on a newly created socket.
 *
 * @param sock      the socket to prepare
 * @param context   the context for the connection
 * @param params    the parameters from which to prepare the socket
 *
 * @throws IOException      in case of an IO problem
 */
protected void prepareSocket(
        final Socket sock,
        final HttpContext context,
        final HttpParams params) throws IOException {
    sock.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
    sock.setSoTimeout(HttpConnectionParams.getSoTimeout(params));

    final int linger = HttpConnectionParams.getLinger(params);
    if (linger >= 0) {
        sock.setSoLinger(linger > 0, linger);
    }
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:22,代码来源:DefaultClientConnectionOperator.java

示例14: create

import java.net.Socket; //导入方法依赖的package包/类
@Override
public HttpClientConnection create(final HttpHost host) throws IOException {
    final String scheme = host.getSchemeName();
    Socket socket = null;
    if ("http".equalsIgnoreCase(scheme)) {
        socket = this.plainfactory != null ? this.plainfactory.createSocket() :
                new Socket();
    } if ("https".equalsIgnoreCase(scheme)) {
        socket = (this.sslfactory != null ? this.sslfactory :
                SSLSocketFactory.getDefault()).createSocket();
    }
    if (socket == null) {
        throw new IOException(scheme + " scheme is not supported");
    }
    final String hostname = host.getHostName();
    int port = host.getPort();
    if (port == -1) {
        if (host.getSchemeName().equalsIgnoreCase("http")) {
            port = 80;
        } else if (host.getSchemeName().equalsIgnoreCase("https")) {
            port = 443;
        }
    }
    socket.setSoTimeout(this.sconfig.getSoTimeout());
    if (this.sconfig.getSndBufSize() > 0) {
        socket.setSendBufferSize(this.sconfig.getSndBufSize());
    }
    if (this.sconfig.getRcvBufSize() > 0) {
        socket.setReceiveBufferSize(this.sconfig.getRcvBufSize());
    }
    socket.setTcpNoDelay(this.sconfig.isTcpNoDelay());
    final int linger = this.sconfig.getSoLinger();
    if (linger >= 0) {
        socket.setSoLinger(true, linger);
    }
    socket.setKeepAlive(this.sconfig.isSoKeepAlive());
    socket.connect(new InetSocketAddress(hostname, port), this.connectTimeout);
    return this.connFactory.createConnection(socket);
}
 
开发者ID:gusavila92,项目名称:java-android-websocket-client,代码行数:40,代码来源:BasicConnFactory.java

示例15: configureSocket

import java.net.Socket; //导入方法依赖的package包/类
/**
 * 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);
    }
}
 
开发者ID:JuanJoseFJ,项目名称:ProyectoPacientes,代码行数:36,代码来源:StandardSocketFactory.java


注:本文中的java.net.Socket.setTcpNoDelay方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。