Java Socket Check isSocketReadyToWrite(Socket socket)

Here you can find the source of isSocketReadyToWrite(Socket socket)

Description

Determines if the socket can be written to.

License

Apache License

Parameter

Parameter Description
socket socket to test

Exception

Parameter Description
IOException an exception

Return

true if the socket is open and can be written to, otherwise false

Declaration

public static boolean isSocketReadyToWrite(Socket socket) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.io.OutputStream;

import java.net.Socket;
import java.net.SocketException;

public class Main {
    /**//from   w  ww.  jav  a2  s  .  com
     * Determines if the socket can be written to. This method tests the writability of the socket by writing to the socket,
     * so it should only be used immediately before closing the socket.
     *
     * @param socket socket to test
     * @return true if the socket is open and can be written to, otherwise false
     * @throws IOException
     */
    public static boolean isSocketReadyToWrite(Socket socket) throws IOException {
        OutputStream out = socket.getOutputStream();
        try {
            out.write(0);
            out.flush();
            out.write(0);
            out.flush();
        } catch (SocketException e) {
            return false;
        }

        return true;
    }
}

Related

  1. isConnected(Socket socket)
  2. isSocketClosed(SocketException e)
  3. isSocketTimeoutException(final InterruptedIOException e)