Java 8 Socket - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Socket class.

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
Connect your IDE to all the code out there  Get Codota for Java
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 
   */ 
9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
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;
  } 
} 
9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
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();
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
        } 
    } 
 
    @Override 
    public Socket accept() throws IOException {
        final Socket socket = metrics ? new ManagedSocketBinding(socketBindings.getUnnamedRegistry()) : new Socket();
        implAccept(socket);
        if(metrics) {
            acceptCount++;
        } 
        return socket;
    } 
 
    @Override 
    public void close() throws IOException { 
        try { 
            super.close(); 
        } finally { 
            if(name != null) {
                socketBindings.getNamedRegistry().unregisterBinding(this);
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
    public void createSocket() throws IOException { 
 
        Socket socket1 = new Socket();
        Socket socket2 = new Socket();
        Socket socket3 = new Socket();
        Socket socket4 = new Socket();
        Socket socket5 = new Socket();
        InetAddress localhost = InetAddress.getLocalHost();
 
        when(this.factory.createSocket("test")).thenReturn(socket1);
        when(this.factory.createSocket("test", localhost, 1)).thenReturn(socket2);
        when(this.factory.createSocket("test", "host", 1)).thenReturn(socket3);
        when(this.factory.createSocket("test", localhost, 1, localhost, 2)).thenReturn(socket4);
        when(this.factory.createSocket("test", "host", 1, localhost, 2)).thenReturn(socket5);
 
        Socket result1 = this.subject.createSocket("test");
        Socket result2 = this.subject.createSocket("test", localhost, 1);
        Socket result3 = this.subject.createSocket("test", "host", 1);
        Socket result4 = this.subject.createSocket("test", localhost, 1, localhost, 2);
        Socket result5 = this.subject.createSocket("test", "host", 1, localhost, 2);

8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getChannel, setSoTimeout
    if (withChannel) {
      s = NetUtils.getDefaultSocketFactory(new Configuration())
          .createSocket(); 
      Assume.assumeNotNull(s.getChannel());
    } else { 
      s = new Socket();
      assertNull(s.getChannel());
    } 
     
    SocketInputWrapper stm = null;
    try { 
      NetUtils.connect(s, ss.getLocalSocketAddress(), 1000);
 
      stm = NetUtils.getInputStream(s, 1000);
      assertReadTimeout(stm, 1000);
 
      // Change timeout, make sure it applies. 
      stm.setTimeout(1);
      assertReadTimeout(stm, 1);
       

8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:isClosed
    */ 
  } 
 
  @Override 
  public boolean isClosed() { 
    return socket.isClosed();
  } 
 
  @Override 
  public void close() throws IOException { 
    socket.close();
  } 
 
  @Override 
  public String getRemoteAddressString() {
    return socket.getRemoteSocketAddress().toString();
  } 
 
  @Override 
  public String getLocalAddressString() {
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:bind, connect
  } 
 
  @Override 
  public Socket createSocket() throws IOException {
 
    return new Socket(proxy);
  } 
 
  @Override 
  public Socket createSocket(InetAddress addr, int port) throws IOException {
 
    Socket socket = createSocket();
    socket.connect(new InetSocketAddress(addr, port));
    return socket;
  } 
 
  @Override 
  public Socket createSocket(InetAddress addr, int port,
      InetAddress localHostAddr, int localPort) throws IOException {
 

8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Java 8 Socket Questions & Answers:

See Code Examples for Java 8 Socket Methods: