Package io.github.astrapi69.net.socket
Klasse SocketExtensions
java.lang.Object
io.github.astrapi69.net.socket.SocketExtensions
Helper class for handling Sockets.
-
Methodenübersicht
Modifizierer und TypMethodeBeschreibungstatic boolean
available
(int port) Checks if the given port at localhost is available.static boolean
Checks if the given port at the given host is available.static void
Close the givenSocket
.static boolean
closeClientSocket
(Socket clientSocket) Closes the given client socket.static boolean
closeServerSocket
(ServerSocket serverSocket) Closes the given ServerSocket.static Socket
Factory method for a socket.static Object
readObject
(int port) Reads an object from the given port.static Object
readObject
(String serverName, int port) Reads an object from the given socket InetAddress.static Object
readObject
(InetAddress inetAddress, int port) Reads an object from the given socket InetAddress.static Object
readObject
(Socket clientSocket) Reads an object from the given socket object.static void
writeObject
(String serverName, int port, Object objectToSend) Writes the given Object.static void
writeObject
(InetAddress inetAddress, int port, Object objectToSend) Writes the given Object to the given InetAddress who listen to the given port.static void
writeObject
(Socket socket, int port, Object objectToSend) Writes the given Object to the given Socket who listen to the given port.
-
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
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
Close the givenSocket
.- Parameter:
clientSocket
- the client socket- Löst aus:
IOException
- Signals that an I/O exception has occurred.
-
closeClientSocket
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
Closes the given ServerSocket.- Parameter:
serverSocket
- The ServerSocket to close.- Gibt zurück:
- Returns true if the ServerSocket is closed otherwise false.
-
newSocket
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
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
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 occurredClassNotFoundException
- 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 addressport
- The port who listen the receiver.objectToSend
- The Object to send.- Löst aus:
IOException
- Signals that an I/O exception has occurred.
-
writeObject
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
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.
-