java.lang.Object
io.github.astrapi69.net.socket.SocketExtensions

public final class SocketExtensions extends Object
Helper class for handling Sockets.
  • Methodendetails

    • available

      public static boolean available(int port)
      Checks if the given port at localhost is available.
      Parameter:
      port - the port number.
      Gibt zurück:
      If the given port at the given host is available true, otherwise false.
    • available

      public static boolean available(String host, int port)
      Checks if the given port at the given host is available. Note: Socket will be closed after the test.
      Parameter:
      host - the host name.
      port - the port number.
      Gibt zurück:
      If the given port at the given host is available true, otherwise false.
    • close

      public static void close(Socket clientSocket) throws IOException
      Close the given Socket.
      Parameter:
      clientSocket - the client socket
      Löst aus:
      IOException - Signals that an I/O exception has occurred.
    • closeClientSocket

      public static boolean closeClientSocket(Socket clientSocket)
      Closes the given client socket.
      Parameter:
      clientSocket - The client socket to close.
      Gibt zurück:
      Returns true if the client socket is closed otherwise false.
    • closeServerSocket

      public static boolean closeServerSocket(ServerSocket serverSocket)
      Closes the given ServerSocket.
      Parameter:
      serverSocket - The ServerSocket to close.
      Gibt zurück:
      Returns true if the ServerSocket is closed otherwise false.
    • newSocket

      public static Socket newSocket(String host, int port) throws IOException
      Factory method for a socket.
      Parameter:
      host - the host name.
      port - the port number.
      Gibt zurück:
      The created Socket.
      Löst aus:
      IOException - is thrown if the port is not available or other network errors.
    • readObject

      public static Object readObject(InetAddress inetAddress, int port) throws IOException, ClassNotFoundException
      Reads an object from the given socket InetAddress.
      Parameter:
      inetAddress - the InetAddress to read.
      port - The port to read.
      Gibt zurück:
      the object
      Löst aus:
      IOException - Signals that an I/O exception has occurred.
      ClassNotFoundException - is thrown if a class of a serialized object cannot be found
    • readObject

      public static Object readObject(int port) throws IOException, ClassNotFoundException
      Reads an object from the given port.
      Parameter:
      port - the port
      Gibt zurück:
      the object
      Löst aus:
      IOException - Signals that an I/O exception has occurred.
      ClassNotFoundException - occurs if a given class cannot be located by the specified class loader
    • readObject

      public static Object readObject(Socket clientSocket) throws IOException, ClassNotFoundException
      Reads an object from the given socket object.
      Parameter:
      clientSocket - the socket to read.
      Gibt zurück:
      the object
      Löst aus:
      IOException - Signals that an I/O exception has occurred
      ClassNotFoundException - is thrown if a class of a serialized object cannot be found
    • readObject

      public static Object readObject(String serverName, int port) throws IOException, ClassNotFoundException
      Reads an object from the given socket InetAddress.
      Parameter:
      serverName - The Name from the address to read.
      port - The port to read.
      Gibt zurück:
      the object
      Löst aus:
      IOException - Signals that an I/O exception has occurred.
      ClassNotFoundException - is thrown if a class of a serialized object cannot be found
    • writeObject

      public static void writeObject(InetAddress inetAddress, int port, Object objectToSend) throws IOException
      Writes the given Object to the given InetAddress who listen to the given port.
      Parameter:
      inetAddress - the inet address
      port - The port who listen the receiver.
      objectToSend - The Object to send.
      Löst aus:
      IOException - Signals that an I/O exception has occurred.
    • writeObject

      public static void writeObject(Socket socket, int port, Object objectToSend) throws IOException
      Writes the given Object to the given Socket who listen to the given port.
      Parameter:
      socket - The Socket to the receiver.
      port - The port who listen the receiver.
      objectToSend - The Object to send.
      Löst aus:
      IOException - Signals that an I/O exception has occurred.
    • writeObject

      public static void writeObject(String serverName, int port, Object objectToSend) throws IOException
      Writes the given Object.
      Parameter:
      serverName - The Name from the receiver(Server).
      port - The port who listen the receiver.
      objectToSend - The Object to send.
      Löst aus:
      IOException - Signals that an I/O exception has occurred.