public final class Client extends Object
Example:
// server ip
Client client = new Client("127.0.0.1");
// add relevant parsers for messages from server
client.addParser(String.class, data -> System.out.println(data));
// connect to server
client.connect();
// send some messages
client.send("This is an example message");
// when done, disconnect from server
client.disconnect();
| Modifier and Type | Field and Description |
|---|---|
protected Map<Class<?>,DataParser<? super Serializable>> |
parsers |
| Constructor and Description |
|---|
Client(String serverIP)
Constructs a new client with given server IP configuration.
|
Client(String serverIP,
int tcpPort,
int udpPort)
Constructs a new client with given server IP and
tcp/udp ports configuration.
|
| Modifier and Type | Method and Description |
|---|---|
<T extends Serializable> |
addParser(Class<T> cl,
DataParser<T> parser)
Register a parser for specified class.
|
boolean |
connect()
Performs an actual connection to the server
|
void |
disconnect()
Sends a message to server that client is
about to disconnect and shuts down connection threads.
|
void |
send(Serializable data)
Send data to the machine at the other end using UDP protocol.
|
void |
send(Serializable data,
NetworkProtocol protocol)
Send data to the machine at the other end using specified protocol
|
protected void |
sendClosingMessage()
Send a message (hint) that this end of connection is about
to close
|
protected void |
sendTCP(Serializable data) |
protected void |
sendUDP(Serializable data) |
protected static byte[] |
toByteArray(Serializable data) |
protected Map<Class<?>,DataParser<? super Serializable>> parsers
public Client(String serverIP)
serverIP - ip of the server machinepublic Client(String serverIP, int tcpPort, int udpPort)
serverIP - ip of the server machinetcpPort - tcp port to useudpPort - udp port to usepublic boolean connect()
throws Exception
Exceptionpublic void disconnect()
Further calls to send(Serializable) will
throw IllegalStateException
protected void sendUDP(Serializable data) throws Exception
Exceptionprotected void sendTCP(Serializable data) throws Exception
Exceptionprotected void sendClosingMessage()
public <T extends Serializable> void addParser(Class<T> cl, DataParser<T> parser)
cl - data structure classparser - the data parserpublic void send(Serializable data) throws Exception
data - the data objectExceptionpublic void send(Serializable data, NetworkProtocol protocol) throws Exception
data - the data objectprotocol - the protocol to useExceptionprotected static byte[] toByteArray(Serializable data) throws Exception
ExceptionCopyright © 2016. All rights reserved.