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


Java Socket.setSoTimeout方法代码示例

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


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

示例1: connectToDN

import java.net.Socket; //导入方法依赖的package包/类
/**
 * Connect to the given datanode's datantrasfer port, and return
 * the resulting IOStreamPair. This includes encryption wrapping, etc.
 */
public static IOStreamPair connectToDN(DatanodeInfo dn, int timeout,
                                       Configuration conf,
                                       SaslDataTransferClient saslClient,
                                       SocketFactory socketFactory,
                                       boolean connectToDnViaHostname,
                                       DataEncryptionKeyFactory dekFactory,
                                       Token<BlockTokenIdentifier> blockToken)
    throws IOException {

  boolean success = false;
  Socket sock = null;
  try {
    sock = socketFactory.createSocket();
    String dnAddr = dn.getXferAddr(connectToDnViaHostname);
    LOG.debug("Connecting to datanode {}", dnAddr);
    NetUtils.connect(sock, NetUtils.createSocketAddr(dnAddr), timeout);
    sock.setSoTimeout(timeout);

    OutputStream unbufOut = NetUtils.getOutputStream(sock);
    InputStream unbufIn = NetUtils.getInputStream(sock);
    IOStreamPair pair = saslClient.newSocketSend(sock, unbufOut,
        unbufIn, dekFactory, blockToken, dn);

    IOStreamPair result = new IOStreamPair(
        new DataInputStream(pair.in),
        new DataOutputStream(new BufferedOutputStream(pair.out,
            NuCypherExtUtilClient.getSmallBufferSize(conf)))
    );

    success = true;
    return result;
  } finally {
    if (!success) {
      IOUtils.closeSocket(sock);
    }
  }
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:42,代码来源:NuCypherExtUtilClient.java

示例2: discardStream

import java.net.Socket; //导入方法依赖的package包/类
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
开发者ID:aabognah,项目名称:LoRaWAN-Smart-Parking,代码行数:28,代码来源:HttpTransport.java

示例3: DefaultConnection

import java.net.Socket; //导入方法依赖的package包/类
/**
 * 创建与服务端连接
 * 
 * @param address
 * @param soTimeout
 * @param connectTimeout
 */
public DefaultConnection(InetSocketAddress address, int soTimeout, int connectTimeout, Charset charset) {
    try {
        socket = new Socket();
        socket.setSoTimeout(soTimeout);
        LOGGER.debug("connect to {} soTimeout={} connectTimeout={}", address, soTimeout, connectTimeout);
        this.charset = charset;
        socket.connect(address, connectTimeout);
    } catch (IOException e) {
        throw new FdfsConnectException("can't create connection to" + address, e);
    }
}
 
开发者ID:whatodo,项目名称:FastDFS_Client,代码行数:19,代码来源:DefaultConnection.java

示例4: run

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

    int trials = 0;
    while(trials < Config.MESSAGE_MAX_TRIALS) {

        try {
            log.debug(p.getPeerServerPort());
            log.debug(p.getAddress());
            Socket messagedClient = new Socket(p.getAddress(), p.getPeerServerPort());
            messagedClient.setSoTimeout(Config.MESSAGE_TIMEOUT);
            ObjectOutputStream out = new ObjectOutputStream(messagedClient.getOutputStream());
            out.writeInt(Config.MESSAGE_OUTGOING);
            out.writeUTF(msg);
            out.flush();

            ObjectInputStream in = new ObjectInputStream(new DataInputStream(messagedClient.getInputStream()));
            int ack = in.readInt();
            messagedClient.close();
            if (ack == Config.MESSAGE_ACK) {
                return;
            }

        } catch (IOException e) {

        }
        trials++;
    }
    log.warn("Message cannot be sent after " + Config.MESSAGE_MAX_TRIALS + " trials");
    log.warn(msg);
}
 
开发者ID:CrypDist,项目名称:CrypDist,代码行数:32,代码来源:MessageTask.java

示例5: run

import java.net.Socket; //导入方法依赖的package包/类
/**
 *
 */
public void run() {
    while(true) {
        try {
            Socket connectionSocket = socket.accept();
            connectionSocket.setSoTimeout(60 * 1000); //Close after 1min to avoid socket leakage
            new ManagerThread(connectionSocket, processController).start();
        } catch(Exception e) {
            log.error("Generic error in server with message: "+e.getMessage());
        }
    }
}
 
开发者ID:mware-solutions,项目名称:sether,代码行数:15,代码来源:ManagerRunnable.java

示例6: initialization

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

        //Establish a connection with server, get number of active peers and their information.
        try {
            Thread t2 = new ReceiveServerRequest(this);
            t2.start();

            Socket serverConnection = new Socket(swAdr, swPort);
            serverConnection.setSoTimeout(Config.SERVER_TIMEOUT);

            DataInputStream in = new DataInputStream(serverConnection.getInputStream());

            receivePeerList(in);

            //Send itself data to server.
            DataOutputStream out = new DataOutputStream(serverConnection.getOutputStream());
            out.writeInt(heartBeatPort);
            out.writeInt(serverPort);
            out.writeUTF(Config.USER_NAME);
            out.writeUTF(Config.USER_PASS);
            out.flush();

            boolean authenticated = in.readBoolean();
            active = in.readBoolean();
            crypDist.setActive(active);
            int size = in.readInt();
            byte[] key_array = new byte[size];
            in.read(key_array);

            crypDist.setSessionKey(key_array);
            crypDist.setAuthenticated(authenticated);

            serverConnection.close();
        }
        catch(IOException e)
        {
            log.warn("Cannot connect to the server, terminated.");
            log.warn(e);
        }
    }
 
开发者ID:CrypDist,项目名称:CrypDist,代码行数:41,代码来源:Client.java

示例7: getSocket

import java.net.Socket; //导入方法依赖的package包/类
/**
 * construct Socket object
 * @param ip_addr ip address or hostname
 * @param port port number
 * @return connected Socket object
 */
public static Socket getSocket(String ip_addr, int port) throws IOException {
    Socket sock = new Socket();
    sock.setSoTimeout(ClientGlobal.g_network_timeout);
    sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
    return sock;
}
 
开发者ID:babymm,项目名称:mumu,代码行数:13,代码来源:ClientGlobal.java

示例8: handshake

import java.net.Socket; //导入方法依赖的package包/类
/**
 * Handshake with the debuggee
 */
void handshake(Socket s, long timeout) throws IOException {
    s.setSoTimeout((int)timeout);

    byte[] hello = "JDWP-Handshake".getBytes("UTF-8");
    s.getOutputStream().write(hello);

    byte[] b = new byte[hello.length];
    int received = 0;
    while (received < hello.length) {
        int n;
        try {
            n = s.getInputStream().read(b, received, hello.length-received);
        } catch (SocketTimeoutException x) {
            throw new IOException("handshake timeout");
        }
        if (n < 0) {
            s.close();
            throw new IOException("handshake failed - connection prematurally closed");
        }
        received += n;
    }
    for (int i=0; i<hello.length; i++) {
        if (b[i] != hello[i]) {
            throw new IOException("handshake failed - unrecognized message from target VM");
        }
    }

    // disable read timeout
    s.setSoTimeout(0);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:SocketTransportService.java

示例9: ping

import java.net.Socket; //导入方法依赖的package包/类
public static PingResult ping(com.badlogic.gdx.net.Socket gdxSocket) throws Exception {
  Socket javaSocket = extractJavaSocket(gdxSocket);
  DataOutputStream dataOutputStream = new DataOutputStream(javaSocket.getOutputStream());
  Long firstTime = System.currentTimeMillis();
  dataOutputStream.writeByte(1); //1 is ping
  javaSocket.setSoTimeout(TIMEOUT);
  try {
    DataInputStream dataInputStream = new DataInputStream(javaSocket.getInputStream());
    PingResult pingResult = new PingResult();
    pingResult.serverMajor = dataInputStream.readInt();
    Long secondTime = System.currentTimeMillis();
    pingResult.serverMinor = dataInputStream.readInt();
    pingResult.serverPoint = dataInputStream.readInt();
    pingResult.serverBuild = dataInputStream.readInt();
    pingResult.serverHash = dataInputStream.readUTF();
    int playerNum = dataInputStream.readInt();
    pingResult.players = new String[playerNum];
    for (int i = 0; i < pingResult.players.length; i++) {
      pingResult.players[i] = dataInputStream.readUTF();
    }
    pingResult.ping = (int) (secondTime - firstTime);
    gdxSocket.dispose();
    return pingResult;
  } catch (IOException e) {
    if (e instanceof SocketTimeoutException) {
      throw new IOException("Server did not respond in time", e);
    } else {
      throw e;
    }
  }
}
 
开发者ID:RedTroop,项目名称:Cubes,代码行数:32,代码来源:ClientConnectionInitializer.java

示例10: initSocket

import java.net.Socket; //导入方法依赖的package包/类
private static Socket initSocket(Config config, InetSocketAddress address) throws IOException {
    Socket socket = new Socket();
    socket.setSoTimeout(config.mTransportTimeOut);
    socket.connect(address, config.mConnectTimeOut);
    socket.setKeepAlive(true);
    return socket;
}
 
开发者ID:android-notes,项目名称:vase,代码行数:8,代码来源:HttpUtils.java

示例11: fillInputBuffer

import java.net.Socket; //导入方法依赖的package包/类
private int fillInputBuffer(final int timeout) throws IOException {
    final Socket socket = this.socketHolder.get();
    final int oldtimeout = socket.getSoTimeout();
    try {
        socket.setSoTimeout(timeout);
        return this.inbuffer.fillBuffer();
    } finally {
        socket.setSoTimeout(oldtimeout);
    }
}
 
开发者ID:kalikov,项目名称:lighthouse,代码行数:11,代码来源:BHttpConnectionBase.java

示例12: createConnection

import java.net.Socket; //导入方法依赖的package包/类
@Override
public DuplexTransportConnection createConnection(ContactId c) {
	if (!isRunning()) return null;
	TransportProperties p = callback.getRemoteProperties().get(c);
	if (p == null) return null;
	String onion = p.get(PROP_ONION);
	if (StringUtils.isNullOrEmpty(onion)) return null;
	if (!ONION.matcher(onion).matches()) {
		// not scrubbing this address, so we are able to find the problem
		if (LOG.isLoggable(INFO)) LOG.info("Invalid hostname: " + onion);
		return null;
	}
	Socket s = null;
	try {
		if (LOG.isLoggable(INFO))
			LOG.info("Connecting to " + scrubOnion(onion));
		controlConnection.forgetHiddenService(onion);
		s = torSocketFactory.createSocket(onion + ".onion", 80);
		s.setSoTimeout(socketTimeout);
		if (LOG.isLoggable(INFO))
			LOG.info("Connected to " + scrubOnion(onion));
		return new TorTransportConnection(this, s);
	} catch (IOException e) {
		if (LOG.isLoggable(INFO)) {
			LOG.info("Could not connect to " + scrubOnion(onion) + ": " +
					e.toString());
		}
		tryToClose(s);
		return null;
	}
}
 
开发者ID:rafjordao,项目名称:Nird2,代码行数:32,代码来源:TorPlugin.java

示例13: openTcpConnect

import java.net.Socket; //导入方法依赖的package包/类
private void openTcpConnect(int timeout) throws S7Exception {
    try {
        tcpSocket = new Socket();
        tcpSocket.connect(new InetSocketAddress(config.getHost(), config.getPort()), timeout);
        tcpSocket.setTcpNoDelay(true);
        tcpSocket.setSoTimeout(recvTimeout);
        inStream = new BufferedInputStream(tcpSocket.getInputStream());
        outStream = new BufferedOutputStream(tcpSocket.getOutputStream());
    } catch (IOException e) {
        throw buildException(TCP_CONNECTION_FAILED, e);
    }
}
 
开发者ID:comtel2000,项目名称:mokka7,代码行数:13,代码来源:S7Client.java

示例14: connect

import java.net.Socket; //导入方法依赖的package包/类
/**
* connect to server
* @return connected Socket object
*/
	public Socket connect() throws IOException
	{
		Socket sock = new Socket();
		sock.setReuseAddress(true);
		sock.setSoTimeout(ClientGlobal.g_network_timeout);
		sock.connect(new InetSocketAddress(this.ip_addr, this.port), ClientGlobal.g_connect_timeout);
		return sock;
	}
 
开发者ID:tb544731152,项目名称:iBase4J,代码行数:13,代码来源:ServerInfo.java

示例15: initSocket

import java.net.Socket; //导入方法依赖的package包/类
private void initSocket()throws Exception{
	socket = new Socket(this.host, this.port);
	socket.setSoTimeout(timeout);
	in = socket.getInputStream();
	out = socket.getOutputStream();
}
 
开发者ID:ahhblss,项目名称:ddpush,代码行数:7,代码来源:Pusher.java


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