Best code examples for Java 8 Socket class (java.net.Socket)

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Socket class.
You can now enable Codota on your own code to easily search and navigate your Java codebase.

Java 8 Socket examples from Open Source projects
This code example shows how to use the following methods: isBound, isClosed, isConnected, isInputShutdown, isOutputShutdown
    public void disconnect() { 
        if (isConnected()) { 
            try { 
                inputStream.close();
                outputStream.close();
                if (!socket.isClosed()) {
                    socket.close();
                } 
            } catch (IOException ex) {
                throw new JedisConnectionException(ex);
            } 
        } 
    } 
 
    public boolean isConnected() { 
        return socket != null && socket.isBound() && !socket.isClosed()
                && socket.isConnected() && !socket.isInputShutdown()
                && !socket.isOutputShutdown();
    } 
 

9
CodeRank
This code example shows how to use the following methods: setSoLinger, setSoTimeout, setTcpNoDelay
 
  /** 
   * Initializes the socket object 
   */ 
  private void initSocket() { 
    socket = new Socket();
    try { 
      socket.setSoLinger(false, 0);
      socket.setTcpNoDelay(true);
      socket.setSoTimeout(timeout);
    } catch (SocketException sx) {
      LOGGER.error("Could not configure socket.", sx);
    } 
  } 
 
  /** 
   * Sets the socket timeout 
   * 
   * @param timeout Milliseconds timeout 
   */ 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: connect, setKeepAlive, setReuseAddress, setSoLinger, setTcpNoDelay
  } 
 
  public static Socket newSocket(String host, int port, int timeout)
    throws IOException 
  { 
    final Socket socket = new Socket();
 
    socket.setReuseAddress(true);
    socket.setKeepAlive(true);  //Will monitor the TCP connection is valid
    socket.setTcpNoDelay(true);  //Socket buffer Whetherclosed, to ensure timely delivery of data
    socket.setSoLinger(true, 0);  //Control calls close () method, the underlying socket is closed immediately
 
    socket.connect(new InetSocketAddress(host, port), timeout);
    //socket.setSoTimeout(timeout); 
 
    return socket;
  } 
} 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: close, getChannel, getInetAddress, getInputStream, getLocalAddress
@InterfaceAudience.Private 
public class TcpPeerServer implements PeerServer { 
  static Log LOG = LogFactory.getLog(TcpPeerServer.class);
 
  private final ServerSocket serverSocket;
 
  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 

9
CodeRank
This code example shows how to use the following methods: getInputStream, getOutputStream
        return ear;
    } 
 
    @Test 
    public void test1() throws Exception { 
        final Socket socket = new Socket(EJBManagementUtil.getNodeName(), 2020);
        final OutputStream sockOut = socket.getOutputStream();
        final DataInputStream in = new DataInputStream(new TelnetInputStream(socket.getInputStream(), sockOut));
        final PrintStream out = new TelnetPrintStream(sockOut);
 
        assertEmptyLine(in);
        assertEquals("type 'help' for a list of commands", in.readLine());
 
        out.println("set a b");
        out.flush();
        assertReply("set a to b", in.readLine());
        assertEmptyLine(in);
 
        out.println("get a");
        out.flush();
Full Snippet Info
8
CodeRank
Related Java 8 Socket Questions & Answers:
See Code Examples for Java 8 Socket Methods: