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


Java Socket.setKeepAlive方法代码示例

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


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

示例1: bind

import java.net.Socket; //导入方法依赖的package包/类
@Override
public void bind(final Socket socket, final HttpParams params) throws IOException {
    if (socket == null) {
        throw new IllegalArgumentException("Socket may not be null");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    assertNotOpen();
    socket.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
    socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
    socket.setKeepAlive(HttpConnectionParams.getSoKeepalive(params));

    int linger = HttpConnectionParams.getLinger(params);
    if (linger >= 0) {
        socket.setSoLinger(linger > 0, linger);
    }
    super.bind(socket, params);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:20,代码来源:DefaultHttpServerConnection.java

示例2: setProperties

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

示例3: setProperties

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

示例4: bind

import java.net.Socket; //导入方法依赖的package包/类
@Override
public void bind(
        final Socket socket,
        final HttpParams params) throws IOException {
    if (socket == null) {
        throw new IllegalArgumentException("Socket may not be null");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    assertNotOpen();
    socket.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
    socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
    socket.setKeepAlive(HttpConnectionParams.getSoKeepalive(params));

    int linger = HttpConnectionParams.getLinger(params);
    if (linger >= 0) {
        socket.setSoLinger(linger > 0, linger);
    }
    super.bind(socket, params);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:22,代码来源:DefaultHttpClientConnection.java

示例5: run

import java.net.Socket; //导入方法依赖的package包/类
@Override
public void run() {
    try {
        while (!isTerminated() && !Thread.interrupted()) {
            final Socket socket = this.serversocket.accept();
            socket.setSoTimeout(this.socketConfig.getSoTimeout());
            socket.setKeepAlive(this.socketConfig.isSoKeepAlive());
            socket.setTcpNoDelay(this.socketConfig.isTcpNoDelay());
            if (this.socketConfig.getRcvBufSize() > 0) {
                socket.setReceiveBufferSize(this.socketConfig.getRcvBufSize());
            }
            if (this.socketConfig.getSndBufSize() > 0) {
                socket.setSendBufferSize(this.socketConfig.getSndBufSize());
            }
            if (this.socketConfig.getSoLinger() >= 0) {
                socket.setSoLinger(true, this.socketConfig.getSoLinger());
            }
            final HttpServerConnection conn = this.connectionFactory.createConnection(socket);
            final Worker worker = new Worker(this.httpService, conn, this.exceptionLogger);
            this.executorService.execute(worker);
        }
    } catch (final Exception ex) {
        this.exceptionLogger.log(ex);
    }
}
 
开发者ID:gusavila92,项目名称:java-android-websocket-client,代码行数:26,代码来源:RequestListener.java

示例6: 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:rafallis,项目名称:BibliotecaPS,代码行数:36,代码来源:StandardSocketFactory.java

示例7: listen

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

示例8: 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

示例9: 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

示例10: 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:how2j,项目名称:lazycat,代码行数:38,代码来源:BioSender.java

示例11: NeoSocketClient

import java.net.Socket; //导入方法依赖的package包/类
public NeoSocketClient(InetAddress inetAddress, int port) {
    gson = new Gson();
    try {
        socket = new Socket(inetAddress, port);
        socket.setKeepAlive(true);
        socket.setSoTimeout(5000);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
开发者ID:neocross,项目名称:NeoSocket,代码行数:11,代码来源:NeoSocketClient.java

示例12: 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:SunburstApps,项目名称:OpenJSharp,代码行数:11,代码来源:DefaultSocketFactoryImpl.java

示例13: run

import java.net.Socket; //导入方法依赖的package包/类
public void run() {
    InputStream clientIn;
    OutputStream clientOut;
    InputStream serverIn;
    OutputStream serverOut;
    try {
        // Connect to the destination server
        mServerSocket = new Socket(
                DESTINATION_HOST,
                DESTINATION_PORT);

        // Turn on keep-alive for both the sockets
        mServerSocket.setKeepAlive(true);
        mClientSocket.setKeepAlive(true);

        // Obtain client & server input & output streams
        clientIn = mClientSocket.getInputStream();
        clientOut = mClientSocket.getOutputStream();
        serverIn = mServerSocket.getInputStream();
        serverOut = mServerSocket.getOutputStream();
    } catch (IOException ioe) {
        System.err.println("Can not connect to " +
                DESTINATION_HOST + ":" +
                DESTINATION_PORT);
        connectionBroken();
        return;
    }

    // Start forwarding data between server and client
    mForwardingActive = true;
    ForwardThread clientForward =
            new ForwardThread(this, clientIn, serverOut);
    clientForward.start();
    ForwardThread serverForward =
            new ForwardThread(this, serverIn, clientOut);
    serverForward.start();

    System.out.println("TCP Forwarding " +
            mClientSocket.getInetAddress().getHostAddress() +
            ":" + mClientSocket.getPort() + " <--> " +
            mServerSocket.getInetAddress().getHostAddress() +
            ":" + mServerSocket.getPort() + " started.");
}
 
开发者ID:PanagiotisDrakatos,项目名称:T0rlib4j,代码行数:44,代码来源:TCPForwardServer.java

示例14: setSockOpts

import java.net.Socket; //导入方法依赖的package包/类
/**
 * Helper method to set socket options.
 * 
 * @param sock
 *            Reference to socket
 */
private void setSockOpts(Socket sock) throws SocketException {
    sock.setTcpNoDelay(true);
    sock.setKeepAlive(tcpKeepAlive);
    sock.setSoTimeout(self.tickTime * self.syncLimit);
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:12,代码来源:QuorumCnxManager.java


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