public static void main(String[] args) throws IOException {
final Server server = new Server();
server.start();
server.bind(12_345);
final Client client = new Client();
client.start();
client.connect(5_000, "localhost", 12_345);
client.addListener(new Listener() {
@Override
public void received(Connection connection, Object object) {
LOG.log(Level.INFO, "Received message {0} in thread {1}", new Object[]{object.toString(), Thread.currentThread().toString()});
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
});
final String text = "test";
final Timer timer = new Timer("timer 1");
timer.schedule(new TimerTask() {
@Override
public void run() {
server.sendToAllTCP(text);
}
}, 0, 100);
final Timer timer2 = new Timer("timer 2");
timer2.schedule(new TimerTask() {
@Override
public void run() {
LOG.log(Level.INFO, "cancel everything");
timer.cancel();
client.stop();
server.stop();
timer2.cancel();
}
}, 2_000);
}