01: package org.xsocket.connection;
02:
03: import java.io.IOException;
04: import java.net.InetAddress;
05: import java.nio.BufferUnderflowException;
06:
07: import org.xsocket.MaxReadSizeExceededException;
08:
09: public final class EchoServer {
10:
11: public static void main(String[] args) throws Exception {
12:
13: new EchoServer().launch(args);
14: }
15:
16: public void launch(String... args) throws Exception {
17:
18: int port = Integer.parseInt(args[0]);
19: int rcvBufferSize = Integer.parseInt(args[1]);
20:
21: IServer server = new Server(InetAddress.getLocalHost(), port,
22: new Handler(rcvBufferSize));
23: ConnectionUtils.start(server);
24:
25: while (true) {
26: try {
27: Thread.sleep(5000);
28: } catch (InterruptedException ignore) {
29: }
30: }
31: }
32:
33: private static final class Handler implements IConnectHandler,
34: IDataHandler {
35:
36: private int rcvBufferSize = 0;
37:
38: Handler(int rcvBufferSize) {
39: this .rcvBufferSize = rcvBufferSize;
40: }
41:
42: public boolean onConnect(INonBlockingConnection connection)
43: throws IOException {
44: connection.setOption(IConnection.SO_RCVBUF, rcvBufferSize);
45: if (((Integer) connection.getOption(IConnection.SO_RCVBUF)) != rcvBufferSize) {
46: System.out.println("not set!");
47: }
48:
49: connection.setOption(IConnection.SO_SNDBUF, rcvBufferSize);
50: if (((Integer) connection.getOption(IConnection.SO_SNDBUF)) != rcvBufferSize) {
51: System.out.println("not set!");
52: }
53: connection.suspendRead();
54: return true;
55: }
56:
57: public boolean onData(INonBlockingConnection connection)
58: throws IOException, BufferUnderflowException,
59: MaxReadSizeExceededException {
60:
61: System.out.println("do nothing");
62:
63: return true;
64: }
65: }
66:
67: }
|