當前位置: 首頁>>代碼示例>>Java>>正文


Java Socket.getSoTimeout方法代碼示例

本文整理匯總了Java中java.net.Socket.getSoTimeout方法的典型用法代碼示例。如果您正苦於以下問題:Java Socket.getSoTimeout方法的具體用法?Java Socket.getSoTimeout怎麽用?Java Socket.getSoTimeout使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.Socket的用法示例。


在下文中一共展示了Socket.getSoTimeout方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

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

示例2: isHealthy

import java.net.Socket; //導入方法依賴的package包/類
boolean isHealthy(boolean doExtensiveChecks) {
    if (!connected) {
        return false;
    }

    Socket socket;
    BufferedSource source;
    synchronized (this) {
        socket = this.socket;
        source = this.source;
    }
    if (socket == null ||
            source == null ||
            socket.isClosed() ||
            socket.isInputShutdown() ||
            socket.isOutputShutdown()) {
        return false;
    }

    if (doExtensiveChecks) {
        try {
            int readTimeout = socket.getSoTimeout();
            try {
                socket.setSoTimeout(1);
                return !source.exhausted();
            } finally {
                socket.setSoTimeout(readTimeout);
            }
        } catch (SocketTimeoutException ignored) {
            ignored.printStackTrace();
            // Read timed out; socket is good.
        } catch (IOException e) {
            return false; // Couldn't read; socket is closed.
        }
    }

    return true;
}
 
開發者ID:pCloud,項目名稱:pcloud-networking-java,代碼行數:39,代碼來源:RealConnection.java

示例3: getSocketTimeout

import java.net.Socket; //導入方法依賴的package包/類
@Override
public int getSocketTimeout() {
    final Socket socket = this.socketHolder.get();
    if (socket != null) {
        try {
            return socket.getSoTimeout();
        } catch (final SocketException ignore) {
            return -1;
        }
    } else {
        return -1;
    }
}
 
開發者ID:gusavila92,項目名稱:java-android-websocket-client,代碼行數:14,代碼來源:BHttpConnectionBase.java

示例4: 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:gusavila92,項目名稱:java-android-websocket-client,代碼行數:11,代碼來源:BHttpConnectionBase.java

示例5: HandShake

import java.net.Socket; //導入方法依賴的package包/類
/**
 * HandShake Constructor used by server side connection
 */
public HandShake(Socket sock, int timeout, DistributedSystem sys, Version clientVersion,
    byte communicationMode) throws IOException, AuthenticationRequiredException {
  this.clientVersion = clientVersion;
  this.system = sys;
  // SocketChannel sc = sock.getChannel();
  /*
   * if (sc != null) { } else
   */ {
    int soTimeout = -1;
    try {
      soTimeout = sock.getSoTimeout();
      sock.setSoTimeout(timeout);
      InputStream is = sock.getInputStream();
      int valRead = is.read();
      // this.code = (byte)is.read();
      if (valRead == -1) {
        throw new EOFException(
            LocalizedStrings.HandShake_HANDSHAKE_EOF_REACHED_BEFORE_CLIENT_CODE_COULD_BE_READ
                .toLocalizedString());
      }
      this.code = (byte) valRead;
      if (this.code != REPLY_OK) {
        throw new IOException(
            LocalizedStrings.HandShake_HANDSHAKE_REPLY_CODE_IS_NOT_OK.toLocalizedString());
      }
      try {
        DataInputStream dis = new DataInputStream(is);
        DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
        this.clientReadTimeout = dis.readInt();
        if (clientVersion.compareTo(Version.CURRENT) < 0) {
          // versioned streams allow object serialization code to deal with older clients
          dis = new VersionedDataInputStream(dis, clientVersion);
          dos = new VersionedDataOutputStream(dos, clientVersion);
        }
        this.id = ClientProxyMembershipID.readCanonicalized(dis);
        // Note: credentials should always be the last piece in handshake for
        // Diffie-Hellman key exchange to work
        if (clientVersion.compareTo(Version.GFE_603) >= 0) {
          setOverrides(new byte[] {dis.readByte()});
        } else {
          setClientConflation(dis.readByte());
        }
        // Hitesh
        if (this.clientVersion.compareTo(Version.GFE_65) < 0
            || communicationMode == Acceptor.GATEWAY_TO_GATEWAY) {
          this.credentials = readCredentials(dis, dos, sys);
        } else {
          this.credentials = this.readCredential(dis, dos, sys);
        }
      } catch (IOException ioe) {
        this.code = -2;
        throw ioe;
      } catch (ClassNotFoundException cnfe) {
        this.code = -3;
        throw new IOException(
            LocalizedStrings.HandShake_CLIENTPROXYMEMBERSHIPID_CLASS_COULD_NOT_BE_FOUND_WHILE_DESERIALIZING_THE_OBJECT
                .toLocalizedString());
      }
    } finally {
      if (soTimeout != -1) {
        try {
          sock.setSoTimeout(soTimeout);
        } catch (IOException ignore) {
        }
      }
    }
  }
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:72,代碼來源:HandShake.java

示例6: readClientVersion

import java.net.Socket; //導入方法依賴的package包/類
private static Version readClientVersion(ServerConnection connection)
    throws IOException, VersionException {

  Socket socket = connection.getSocket();
  int timeout = connection.getHandShakeTimeout();

  int soTimeout = -1;
  try {
    soTimeout = socket.getSoTimeout();
    socket.setSoTimeout(timeout);
    InputStream is = socket.getInputStream();
    short clientVersionOrdinal = Version.readOrdinalFromInputStream(is);
    if (clientVersionOrdinal == -1) {
      throw new EOFException(
          LocalizedStrings.ServerHandShakeProcessor_HANDSHAKEREADER_EOF_REACHED_BEFORE_CLIENT_VERSION_COULD_BE_READ
              .toLocalizedString());
    }
    Version clientVersion = null;
    try {
      clientVersion = Version.fromOrdinal(clientVersionOrdinal, true);
    } catch (UnsupportedVersionException uve) {
      // Allows higher version of wan site to connect to server
      if (connection.getCommunicationMode() == Acceptor.GATEWAY_TO_GATEWAY
          && !(clientVersionOrdinal == Version.NOT_SUPPORTED_ORDINAL)) {
        return Acceptor.VERSION;
      } else {
        SocketAddress sa = socket.getRemoteSocketAddress();
        String sInfo = "";
        if (sa != null) {
          sInfo = " Client: " + sa.toString() + ".";
        }
        throw new UnsupportedVersionException(uve.getMessage() + sInfo);
      }
    }

    if (!clientVersion.compatibleWith(Acceptor.VERSION)) {
      throw new IncompatibleVersionException(clientVersion, Acceptor.VERSION);// we can throw this
                                                                              // to restrict
    } // Backward Compatibilty Support to limited no of versions
    return clientVersion;
  } finally {
    if (soTimeout != -1) {
      try {
        socket.setSoTimeout(soTimeout);
      } catch (IOException ignore) {
      }
    }
  }
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:50,代碼來源:ServerHandShakeProcessor.java

示例7: SocketInputStream

import java.net.Socket; //導入方法依賴的package包/類
/**
 * Same as SocketInputStream(socket.getChannel(), socket.getSoTimeout())
 * :<br><br>
 * 
 * Create a new input stream with the given timeout. If the timeout
 * is zero, it will be treated as infinite timeout. The socket's
 * channel will be configured to be non-blocking.
 * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long)
 *  
 * @param socket should have a channel associated with it.
 * @throws IOException
 */
public SocketInputStream(Socket socket) throws IOException {
  this(socket.getChannel(), socket.getSoTimeout());
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:16,代碼來源:SocketInputStream.java


注:本文中的java.net.Socket.getSoTimeout方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。