Java Code Examples for java.net.Socket.getSoTimeout()

The following are Jave code examples for showing how to use getSoTimeout() of the java.net.Socket class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: LoRaWAN-Smart-Parking   File: HttpTransport.java   Source Code and License Vote up 6 votes
/**
 * 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;
  }
}
 
Example 2
Project: LoRaWAN-Smart-Parking   File: HttpTransport.java   Source Code and License Vote up 6 votes
/**
 * 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;
  }
}
 
Example 3
Project: pcloud-networking-java   File: RealConnection.java   Source Code and License Vote up 5 votes
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;
}
 
Example 4
Project: java-android-websocket-client   File: BHttpConnectionBase.java   Source Code and License Vote up 5 votes
@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;
    }
}
 
Example 5
Project: java-android-websocket-client   File: BHttpConnectionBase.java   Source Code and License Vote up 5 votes
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);
    }
}
 
Example 6
Project: lighthouse   File: BHttpConnectionBase.java   Source Code and License Vote up 5 votes
@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;
    }
}
 
Example 7
Project: lighthouse   File: BHttpConnectionBase.java   Source Code and License Vote up 5 votes
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);
    }
}
 
Example 8
Project: monarch   File: HandShake.java   Source Code and License Vote up 4 votes
/**
 * 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) {
        }
      }
    }
  }
}
 
Example 9
Project: monarch   File: ServerHandShakeProcessor.java   Source Code and License Vote up 4 votes
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) {
      }
    }
  }
}
 
Example 10
Project: hadoop-oss   File: SocketInputStream.java   Source Code and License Vote up 2 votes
/**
 * 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());
}
 
Example 11
Project: spark_deep   File: SocketInputStream.java   Source Code and License Vote up 2 votes
/**
 * 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());
}
 
Example 12
Project: hadoop   File: SocketInputStream.java   Source Code and License Vote up 2 votes
/**
 * 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());
}