public final class Server extends Object
Example:
// create server object with default params, note: no network operation yet
Server server = new Server();
// add relevant parsers for messages from client
server.addParser(String.class, data -> System.out.println(data));
// actual network operation
server.start();
// send some messages
server.send("This is an example message");
// when done, stop the server
server.stop();
| Modifier and Type | Field and Description |
|---|---|
protected Map<Class<?>,DataParser<? super Serializable>> |
parsers |
| Constructor and Description |
|---|
Server()
Constructs and configures a single-client server with default ports
No network operation is done at this point.
|
Server(int tcpPort,
int udpPort)
Constructs and configures a single-client server with default ports
No network operation is done at this point.
|
| Modifier and Type | Method and Description |
|---|---|
<T extends Serializable> |
addParser(Class<T> cl,
DataParser<T> parser)
Register a parser for specified class.
|
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) |
void |
start()
Starts the server.
|
void |
stop()
Sends a message to all connected clients that
the server is about to shut down.
|
protected static byte[] |
toByteArray(Serializable data) |
protected Map<Class<?>,DataParser<? super Serializable>> parsers
public Server()
public Server(int tcpPort,
int udpPort)
public void start()
public void stop()
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.