Package io.github.astrapi69.net.socket
Class SocketExtensions
- java.lang.Object
-
- io.github.astrapi69.net.socket.SocketExtensions
-
public class SocketExtensions extends java.lang.Object
Helper class for handling Sockets.
-
-
Constructor Summary
Constructors Constructor Description SocketExtensions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
available(int port)
Checks if the given port at localhost is available.static boolean
available(java.lang.String host, int port)
Checks if the given port at the given host is available.static void
close(java.net.Socket clientSocket)
Close the givenSocket
.static boolean
closeClientSocket(java.net.Socket clientSocket)
Closes the given client socket.static boolean
closeServerSocket(java.net.ServerSocket serverSocket)
Closes the given ServerSocket.static java.net.Socket
newSocket(java.lang.String host, int port)
Factory method for a socket.static java.lang.Object
readObject(int port)
Reads an object from the given port.static java.lang.Object
readObject(java.lang.String serverName, int port)
Reads an object from the given socket InetAddress.static java.lang.Object
readObject(java.net.InetAddress inetAddress, int port)
Reads an object from the given socket InetAddress.static java.lang.Object
readObject(java.net.Socket clientSocket)
Reads an object from the given socket object.static void
writeObject(java.lang.String serverName, int port, java.lang.Object objectToSend)
Writes the given Object.static void
writeObject(java.net.InetAddress inetAddress, int port, java.lang.Object objectToSend)
Writes the given Object to the given InetAddress who listen to the given port.static void
writeObject(java.net.Socket socket, int port, java.lang.Object objectToSend)
Writes the given Object to the given Socket who listen to the given port.
-
-
-
Method Detail
-
available
public static boolean available(int port)
Checks if the given port at localhost is available.- Parameters:
port
- the port number.- Returns:
- If the given port at the given host is available true, otherwise false.
-
available
public static boolean available(java.lang.String host, int port)
Checks if the given port at the given host is available. Note: Socket will be closed after the test.- Parameters:
host
- the host name.port
- the port number.- Returns:
- If the given port at the given host is available true, otherwise false.
-
close
public static void close(java.net.Socket clientSocket) throws java.io.IOException
Close the givenSocket
.- Parameters:
clientSocket
- the client socket- Throws:
java.io.IOException
- Signals that an I/O exception has occurred.
-
closeClientSocket
public static boolean closeClientSocket(java.net.Socket clientSocket)
Closes the given client socket.- Parameters:
clientSocket
- The client socket to close.- Returns:
- Returns true if the client socket is closed otherwise false.
-
closeServerSocket
public static boolean closeServerSocket(java.net.ServerSocket serverSocket)
Closes the given ServerSocket.- Parameters:
serverSocket
- The ServerSocket to close.- Returns:
- Returns true if the ServerSocket is closed otherwise false.
-
newSocket
public static java.net.Socket newSocket(java.lang.String host, int port) throws java.io.IOException
Factory method for a socket.- Parameters:
host
- the host name.port
- the port number.- Returns:
- The created Socket.
- Throws:
java.io.IOException
- is thrown if the port is not available or other network errors.
-
readObject
public static java.lang.Object readObject(java.net.InetAddress inetAddress, int port) throws java.io.IOException, java.lang.ClassNotFoundException
Reads an object from the given socket InetAddress.- Parameters:
inetAddress
- the InetAddress to read.port
- The port to read.- Returns:
- the object
- Throws:
java.io.IOException
- Signals that an I/O exception has occurred.java.lang.ClassNotFoundException
- is thrown if a class of a serialized object cannot be found
-
readObject
public static java.lang.Object readObject(int port) throws java.io.IOException, java.lang.ClassNotFoundException
Reads an object from the given port.- Parameters:
port
- the port- Returns:
- the object
- Throws:
java.io.IOException
- Signals that an I/O exception has occurred.java.lang.ClassNotFoundException
- occurs if a given class cannot be located by the specified class loader
-
readObject
public static java.lang.Object readObject(java.net.Socket clientSocket) throws java.io.IOException, java.lang.ClassNotFoundException
Reads an object from the given socket object.- Parameters:
clientSocket
- the socket to read.- Returns:
- the object
- Throws:
java.io.IOException
- Signals that an I/O exception has occurredjava.lang.ClassNotFoundException
- is thrown if a class of a serialized object cannot be found
-
readObject
public static java.lang.Object readObject(java.lang.String serverName, int port) throws java.io.IOException, java.lang.ClassNotFoundException
Reads an object from the given socket InetAddress.- Parameters:
serverName
- The Name from the address to read.port
- The port to read.- Returns:
- the object
- Throws:
java.io.IOException
- Signals that an I/O exception has occurred.java.lang.ClassNotFoundException
- is thrown if a class of a serialized object cannot be found
-
writeObject
public static void writeObject(java.net.InetAddress inetAddress, int port, java.lang.Object objectToSend) throws java.io.IOException
Writes the given Object to the given InetAddress who listen to the given port.- Parameters:
inetAddress
- the inet addressport
- The port who listen the receiver.objectToSend
- The Object to send.- Throws:
java.io.IOException
- Signals that an I/O exception has occurred.
-
writeObject
public static void writeObject(java.net.Socket socket, int port, java.lang.Object objectToSend) throws java.io.IOException
Writes the given Object to the given Socket who listen to the given port.- Parameters:
socket
- The Socket to the receiver.port
- The port who listen the receiver.objectToSend
- The Object to send.- Throws:
java.io.IOException
- Signals that an I/O exception has occurred.
-
writeObject
public static void writeObject(java.lang.String serverName, int port, java.lang.Object objectToSend) throws java.io.IOException
Writes the given Object.- Parameters:
serverName
- The Name from the receiver(Server).port
- The port who listen the receiver.objectToSend
- The Object to send.- Throws:
java.io.IOException
- Signals that an I/O exception has occurred.
-
-