Class SocketUtil


  • public class SocketUtil
    extends Object
    This class contains some useful methods when working with sockets. Mainly for easy reading and writing to/from the sockets, and also for closing sockets without throwing exceptions.
    Version:
    2.0
    Author:
    nicklas
    Last modified
    $Date: 2011-10-12 14:45:07 +0200 (on, 12 okt 2011) $
    • Constructor Detail

      • SocketUtil

        public SocketUtil()
    • Method Detail

      • send

        public static void send​(Socket socket,
                                String output,
                                boolean shutdownOutput)
                         throws IOException
        Send output to a socket.
        Parameters:
        socket - The socket to send to
        output - The output to send
        shutdownOutput - TRUE to close the output stream, FALSE to leave it open
        Throws:
        IOException - If there is an error
      • read

        public static String read​(Socket socket,
                                  boolean shutdownInput)
                           throws IOException
        Read input from a socket.
        Parameters:
        socket - The socket to read from
        shutdownInput - If the input stream should be closed or not
        Returns:
        The data that was read
        Throws:
        IOException - If there is an error
      • close

        public static void close​(Socket socket)
        Close a socket without throwing any exceptions.
        Parameters:
        socket - The socket to close
      • close

        public static void close​(ServerSocketChannel socket)
        Close a server socket channel without throwing any exceptions.
        Parameters:
        socket - The socket to close
      • close

        public static void close​(ServerSocket socket)
        Close a server socket without throwing any exceptions.
        Parameters:
        socket - The socket to close
      • close

        public static void close​(Writer out)
        Close a Writer without throwing any exceptions.
        Parameters:
        out - The writer to close
      • close

        public static void close​(Reader in)
        Close a Reader without throwing any exceptions.
        Parameters:
        in - The reader to close
      • close

        public static void close​(InputStream in)
        Close an InputStream without throwing any exceptions.
        Parameters:
        in - The stream to close
      • isLocalHost

        public static boolean isLocalHost​(InetAddress address)
        Check if the specfied address is the local host or not.
        Parameters:
        address - The address to check
        Returns:
        TRUE if the address represents the local host, FALSE otherwise
      • getLocalHost

        public static InetAddress getLocalHost()
        Get the local address of the local host.
      • getPublicLocalHost

        public static InetAddress getPublicLocalHost()
        Get the external address of the local host.