Posted on 2007-05-28 15:41
change 阅读(131)
评论(0) 编辑 收藏
JXTA 双向通讯 可以通过 JxtaServerSocket /JxtaSocket和 JxtaServerPipe/JxtaBiDiPipe 来实现 其实现的过程非常的类是我们做FTP的时候所采用的ServerSocket/Socket机制,也就是服务断监听客户端连接的原理。以JxtaServerPipe为例,在服务端:
serverPipe = new JxtaServerPipe(eg.netPeerGroup,eg.pipeAdv);
serverPipe.setPipeTimeout(0);然后就是服务端的循环监听客户端的连接
while (true) {
try {
JxtaBiDiPipe bipipe = serverPipe.accept();
if (bipipe != null ) {
System.out.println("JxtaBidiPipe accepted,sending 100 messages to the other end");
//Send a 100 messages
sendTestMessages(bipipe);
}
} catch (Exception e) {
}
}
在客户端则是通过JxtaBiDiPipe 来进行连接服务断的操作:pipe = new JxtaBiDiPipe();
pipe.connect(eg.netPeerGroup,null,eg.pipeAdv,
180000,
// register as a message listener
eg);当有消息来得时候就会触发 pipeMsgEvent(PipeMsgEvent event)事件