http://cindy.sourceforge.net
final Socket socket = ss.accept();
new Thread() {
public void run() {
BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
socket.close();
};
}.start();
}
3. 基于 Cindy 的同步客户端示例
Session session = SessionFactory.createSocketSession();
session.setRemoteAddress(new InetSocketAddress("localhost", 1234));
session.start().complete();
Packet packet = new DefaultPacket(BufferFactory.wrap("Hello world!".getBytes()));
session.flush (packet).complete();
session.close().complete();
Step 1: the Session interface
Session 是网络连接的抽象,每个 Session 代表着一个连接。SocketSession 代表着 TCP 连
接,DatagramSession 代表着 UDP 连接,PipeSession 代表着 Pipe 连接等等。
在这里通过 SessionFactory.createSocketSession()创建了一个 TCP 连接。
Step 2: set the remote address
TCP 需要先设置好要连接的地址才能开始连接,在这里是连接到本机的 1234 端口。
Step 3: start session
参数设置完成后就可以开始连接了。和 Java IO 中的阻塞同步调用不同的是,由于 Cindy