Package rosequartz.net
Class TCPSocket<T extends Serializable>
java.lang.Object
rosequartz.net.TCPSocket<T>
- Type Parameters:
T
- the type of data to send. To send pure string data, use the String-type.
Represents a socket using the TCP protocol.
Sockets are used to communicate between computers.
The TCP protocol guarantees that all messages reach their destination.
WARNING! TCPSocket is not supported by the browser runtime. Only use this class if you do NOT plan on releasing your project for browsers.
WARNING! TCPSocket is not supported by the browser runtime. Only use this class if you do NOT plan on releasing your project for browsers.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Represents the current state of the socket. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaccept()
Launches a new thread and accepts one connection from any socket on the configured listener port.Accepts one connection from any socket on the configured listener port, blocking this thread until a connection is established.close()
Closes the socket, ending its current connection.Launches a new thread and connects this socket to another socket given its address and port.connectBlocking(String address, int port)
Connects this socket to another socket given its address and port, blocking this thread until a connection is established.Gets the address of this socket.Gets the address of the socket this socket is connected to.int
Gets the port of the socket this socket is connected to.int
getPort()
Gets the current listener port of the socket.getState()
Gets the current state of the socket.protected void
Method called when the socket is closed.Set a function to be called when the socket is closed.protected void
Method called when a connection is established.Set a function to be called when the socket is connected.Set a function to be called when a message is received.protected void
Method called when a message is received.Sends a message to the connected socket.setPort(int port)
Sets the listener port of the socket.
-
Constructor Details
-
TCPSocket
Creates a new TCP-Socket with a random listener port.- Throws:
PlatformSpecificDisabledException
- if not enabled
-
TCPSocket
Creates a new TCP-Socket from a listener port.- Parameters:
port
- the port to listen on- Throws:
PlatformSpecificDisabledException
- if not enabled
-
-
Method Details
-
setPort
Sets the listener port of the socket.- Parameters:
port
- listener port- Returns:
- this
- Throws:
IllegalStateException
- if the socket is not closed
-
connect
Launches a new thread and connects this socket to another socket given its address and port.- Parameters:
address
- the address of the socket to connect toport
- the port of the socket to connect to- Returns:
- this
- Throws:
IllegalStateException
- if the socket is not closed
-
connectBlocking
Connects this socket to another socket given its address and port, blocking this thread until a connection is established.- Parameters:
address
- the address of the socket to connect toport
- the port of the socket to connect to- Returns:
- this
- Throws:
IllegalStateException
- if the socket is not closed
-
accept
Launches a new thread and accepts one connection from any socket on the configured listener port. To handle multiple connections to sockets from the same port, create a new socket with the same port.- Returns:
- this
- Throws:
IllegalStateException
- if the socket is not closed
-
acceptBlocking
Accepts one connection from any socket on the configured listener port, blocking this thread until a connection is established. To handle multiple connections to sockets from the same port, create a new socket with the same port.- Returns:
- this
- Throws:
IllegalStateException
- if the socket is not closed
-
onConnect
Set a function to be called when the socket is connected.- Parameters:
action
- function to call on socket connection- Returns:
- this
-
onConnect
protected void onConnect()Method called when a connection is established. By default, this method calls the configured function by 'onConnect(Runnable action)', but it may be overridden. -
onMessage
Set a function to be called when a message is received.- Parameters:
action
- function to call on message receive- Returns:
- this
-
onMessage
Method called when a message is received. By default, this method calls the configured function by 'onMessage(Consumeraction)', but it may be overridden. -
onClose
Set a function to be called when the socket is closed.- Parameters:
action
- function to call on socket close- Returns:
- this
-
onClose
Method called when the socket is closed. By default, this method calls the configured function by 'onClose(Consumeraction)', but it may be overridden. -
send
Sends a message to the connected socket.- Parameters:
message
- message to send- Returns:
- this
- Throws:
IllegalStateException
- if the socket is not connected
-
close
Closes the socket, ending its current connection.- Returns:
- this
- Throws:
IllegalStateException
- if the socket is closed
-
getState
Gets the current state of the socket.- Returns:
- state
-
getPort
public int getPort()Gets the current listener port of the socket.- Returns:
- listener port
-
getAddress
Gets the address of this socket. -
getConnectedPort
public int getConnectedPort()Gets the port of the socket this socket is connected to.- Throws:
IllegalStateException
- if the socket is not connected
-
getConnectedAddress
Gets the address of the socket this socket is connected to.- Throws:
IllegalStateException
- if the socket is not connected
-