- 浏览: 229867 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
-
annyliyear:
找到的ExtJS实现多选下拉框3个代码 -
yinboxian:
源码太乱了
JXTA中使用JxtaServerPipe和JxtaBiDiPipe实现双向可靠的通信 -
yinboxian:
我的peers第一次执行时可以得到正确的结果,以后就不行了。不 ...
JXTA中基于管道监听方式通信 -
cuizhenfu#gmail.com:
http://www.cnblogs.com/cuizhf
JXTA中使用JxtaServerPipe和JxtaBiDiPipe实现双向可靠的通信 -
luxing44530:
请问, jsp页面如果在 META-INF 中, 改怎么访问? ...
Servlet3.0 新特性之模块化实践
本文主要讲解的是如何单纯发布管道广告,及利用发现的管道进行简单的信息通信。PeerServer发布管道广告,等待其它PeerClient发现及连接,然后向PeerServer发送一条内容为“你是谁”的信息。下面给出的两个类可以放在同个目录下运行。
public class PeerServer {
private DiscoveryService discoveryService = null;
private PipeService pipeService = null;
private PeerGroup restoNet = null;
private PeerGroupID peerGroupID = null;
private InputPipe inputPipe = null;
public static void main(String[] args) {
Logger.getLogger("net.jxta").setLevel(Level.SEVERE);
PeerServer peer1 = new PeerServer();
peer1.launchJXTA();
}
private void launchJXTA() {
try {
NetworkConfigurator config = new NetworkConfigurator();//设置配置可以跳过第1次运行时出现的配置UI
config.setPrincipal("peer1");// Peer名称
config.setPassword("888888888");// Peer密码
config.save();
restoNet = new NetPeerGroupFactory().getInterface();
peerGroupID = restoNet.getPeerGroupID();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
discoveryService = restoNet.getDiscoveryService();// 取得NetPeerGroup的发现服务
pipeService = restoNet.getPipeService();// 取得NetPeerGroup的管道服务
startServer();// 开始启动
}
private void startServer() {
publishPipeAdvertisement();// 发布管道广告
}
//创建及发布管道广告
private void publishPipeAdvertisement() {
PipeAdvertisement pipeAdv = createPipeAdvertisement();
// -----------------以下这段代码只是为了把管道广告的内容打印出来--------------------------
StructuredTextDocument doc = (StructuredTextDocument) pipeAdv
.getDocument(MimeMediaType.XMLUTF8);
StringWriter out = new StringWriter();
try {
doc.sendToWriter(out);
System.out.println(out.toString());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
// -----------------------------打印结束--------------------------------------------
try {
discoveryService.publish(pipeAdv);//本地发布管道广告
discoveryService.remotePublish(pipeAdv);//远程发布管道广告
inputPipe = pipeService.createInputPipe(pipeAdv);// 创建输入管道
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
System.out.println("等待其它Peer端信息的到达.........");
Message msg;
try {
msg = inputPipe.waitForMessage();// 监听输入管道是否有信息传进来
} catch (InterruptedException e) {
inputPipe.close();
System.out.println("接收其它Peer信息出错!");
return;// 如果出现异常则返回
}
String receiveContent = null;
Message.ElementIterator en = msg.getMessageElements();// 取得到信息
if (!en.hasNext()) {
return;
}
MessageElement msgElement = msg.getMessageElement(null, "DataTag");
if (msgElement.toString() != null) {
receiveContent = msgElement.toString();
}
if (receiveContent != null) {
System.out.println("接收信息内容: "
+ receiveContent);
} else {
System.out
.println("没有内容");
}
}
}
// 生成管道广告,在这里我们是直接从代码中生成管道广告,当然我们可以读管道广告文件
private PipeAdvertisement createPipeAdvertisement() {
PipeAdvertisement pipeAdvertisement = null;
pipeAdvertisement = (PipeAdvertisement) AdvertisementFactory
.newAdvertisement(PipeAdvertisement.getAdvertisementType());// 创建一个管道广告
pipeAdvertisement.setPipeID(createPipeID(peerGroupID));
pipeAdvertisement.setName("Pipe");
pipeAdvertisement.setDescription("JXTA create first pipe");
pipeAdvertisement.setType(PipeService.UnicastType);// 管道类型,管道类型在JXTA
return pipeAdvertisement;
}
// 生成管道ID
private PipeID createPipeID(PeerGroupID groupID) {
PipeID pipeID = null;
pipeID = IDFactory.newPipeID(groupID);
return pipeID;
}
}
先执行上面这个类,再执行下面PeerClient类。
public class PeerClient {
private PeerGroup netpg = null;// PeerGroup
private DiscoveryService disco; // 发现服务
private PipeService pipeSev; // 管道服务
private PipeAdvertisement pipeAdv = null;// 管道广告
private OutputPipe outputPipe;// 输入管道
public static void main(String[] args) {
Logger.getLogger("net.jxta").setLevel(Level.SEVERE);
PeerClient peer2 = new PeerClient();
peer2.launchJXTA();
}
private void launchJXTA() {
System.out.println("Lauching Peer into JXTA NetWork...");
try {
NetworkConfigurator config = new NetworkConfigurator();
config.setPrincipal("peer2");
config.setPassword("888888888");
config.save();
netpg = new NetPeerGroupFactory().getInterface();
} catch (Exception e) {
System.out.println("Unable to create PeerGroup - Failure");
System.exit(-1);
}
startClient();
}
private void startClient() {
System.out.println("搜索管道广告....");
disco = netpg.getDiscoveryService();//获取NetGroup发现服务
pipeSev = netpg.getPipeService();//获取NetGroup管道服务
Enumeration en;
while (true) {
try {
en = disco.getLocalAdvertisements(DiscoveryService.ADV, "Name",
"Pipe");// 本地发现广告,后面对应了广告中Name标签,值为Pipe的管道广告
if ((en != null) && en.hasMoreElements()) {
break;
}
disco.getRemoteAdvertisements(null, DiscoveryService.ADV,
"Name", "Pipe", 1, null);// 远程发现广告
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(".");
}
System.out.println("已经找到管道广告.......");
pipeAdv = (PipeAdvertisement) en
.nextElement();
if (null == pipeAdv) {
System.out.println("没有找到管道广告");
}
try {
outputPipe = pipeSev.createOutputPipe(pipeAdv, 10000);// 创建输出管道,其实是连接Peer1中的输入管道
} catch (IOException e) {
e.printStackTrace();
}
String data = "你是谁";// 我们要发送的信息内容
Message msg = new Message();
StringMessageElement sme = new StringMessageElement("DataTag", data,
null);
msg.addMessageElement(null, sme);
try {
outputPipe.send(msg);// 发送信息
System.out.println("信息 \"" + data
+ "\" 已经发送");
} catch (IOException e) {
e.printStackTrace();
System.out
.println("发送信息失败!!");
}
}
}
评论
我也发现了这个问题,第一次启动可以成功,第二次就报错了。我是把项目文件夹下面的".jxta"文件夹删除了再启动就好了,现在还没弄清楚到底什么原因。
实现广告是什么意思?
Lauching Peer into JXTA NetWork...
搜索管道广告....
已经找到管道广告.......
java.io.IOException: Output Pipe could not be resolved after 10000ms.
at net.jxta.impl.pipe.PipeServiceImpl.createOutputPipe(PipeServiceImpl.java:459)
at net.jxta.impl.pipe.PipeServiceImpl.createOutputPipe(PipeServiceImpl.java:416)
at net.jxta.impl.pipe.PipeServiceInterface.createOutputPipe(PipeServiceInterface.java:160)
at jxta_comunication.PeerClient.startClient(PeerClient.java:84)
at jxta_comunication.PeerClient.launchJXTA(PeerClient.java:51)
at jxta_comunication.PeerClient.main(PeerClient.java:36)
Exception in thread "main" java.lang.NullPointerException
at jxta_comunication.PeerClient.startClient(PeerClient.java:95)
at jxta_comunication.PeerClient.launchJXTA(PeerClient.java:51)
at jxta_comunication.PeerClient.main(PeerClient.java:36)
应该就是Output连不上了。请问这是什么问题?
你好 我也通过你提示我的信息找了相关的资料,其中 即把管道广告包含在实现广告中发布。 我做了测试 这种情况只有两端都建立默认组的情况下才能实现。如果一方建立组 另外一方加入此组 就找不到PipeAdvertisement. 也不知道是什么情况导致的。难道两个PEER在默认组 和两个PEER在自己建立的peergroup 是不一样的、
config.addSeedRelay(new URI("http://192.168.0.9:9702"),10);
config.addSeedRendezvous(new URI("http://192.168.0.9:9702"),10);
可能是我说的不太清楚,我用你的例子在一个远程服务器上建立了服务端,也就是你写的PEERSERVER类 在服务端运行
我的客户端是在我公司的子网里面 现在在子网运行你的PEERCILENT类时 互相都搜索不到对方。
另外像你这样的用代码跳过配置窗口。不用给服务器端配置Rendezvous 让服务器作为Rendezvous吗
我关于JXTA配置窗口的配置如下 不知道对不对 请牙哥指点下
服务器设置 在Basic 中 填写 peername 和 password
在 Advanced 中把服务器设置为 Act as a Rendezvous
TCP Settings中 给Manual设置自己的IP 加9701端口
在Rendezvous/Relays中 取消Use a rendezvous 和 Use a relay复选框
客户端设置
在Basic 中 填写 peername 和 password
在 Advanced 中 什么都不改
在Rendezvous/Relays中
在 Use a rendezvous 的 Rendezvous seed peers中填写
tcp://服务器IP:9701
取消 Use a relay复选框
我现在不清楚要是连接远程服务器是不是我配置的有问题。
你说的:“要用标准的方法来连接。即把管道广告包含在实现广告中发布。”
的标准方法连接是什么意思 普通连接是什么呢 。后面的意思有是什么呢?
我们平时发布广告不是用
DiscoveryService.publish 来发布本地广告
DiscoveryService.remotePublish 来发布远程广告
你说的实现广告中发布是什么意思呢?
发表评论
-
(JXTA P2P)利用监听DiscoveryListener发现远程广告
2010-05-17 21:47 1521最近朋友们一直都是问 ... -
JXTA中使用JxtaServerPipe和JxtaBiDiPipe实现双向可靠的通信
2010-04-14 00:18 1685前面我们讲到如何在JXTA中创建单向不可靠的通信渠道,本节我们 ... -
JXTA如何创建自定义的广告
2010-04-13 01:43 1635广告是JXTA核心对象,它是用来公布对等机,对等机,服务,管道 ... -
JXTA中双向可靠的信息渠道
2010-04-11 16:11 1066在前面我们讲到管道通信都是单向不可靠的。如果JXTA只提供它, ... -
JXTA中通过身份认证来加入“群”(三)
2010-04-10 19:05 1211本文是接前面两篇,结束本篇就完成密码对等组创建,发现及加入的过 ... -
JXTA中通过身份认证来加入“群”(二)
2010-04-10 15:44 1090本文是接前篇而写,所以一些前面讲过的东西在这里不再重复。想了解 ... -
JXTA中通过身份认证来加入“群”(一)
2010-04-10 11:28 1339之前讲的加入“群”,即对等组。都是不用认证的,只要发现了该“群 ... -
JXTA中基于管道监听方式通信
2010-04-09 21:25 1567在上一篇中“基于Pipe管 ... -
在JXTA中如何搜索目标对等组中的所有Peer
2010-04-07 23:22 1397在JXTA中发布和发现任何资源都是根据广告,因此,我们要发现P ... -
如何从HelloWorld开始学习JXTA
2010-04-07 22:12 2554本文主要从代码来讲解 ... -
JXTA的“名片”之一PeerAdvertisement(对等机广告)
2010-04-06 20:47 1254PeerAdvertisement(对等机广告)有两个最终的目 ... -
JXTA中的“名片”
2010-04-06 19:27 1324JXTA中的所谓“名片”,指的是JXTA语言中的广告,P2P网 ...
相关推荐
进程间通信的一种方式,Pipes:管道,分为无名管道:在父子进程间交换数据;有名管道:可在不同主机间交换数据,分为服务器方和客户方,在Win9X下只支持有名管道客户。 1、进程间通信 2、管道Pipe通信
visual c++利用命名管道namedpipe进行通信
编制一段程序,实现进程的管道通信,使用系统调用pipe()建立一个管道文件;两个子进程P1和P2 分别向管道各写一句话: Child1 is sending a message! Child2 is sending a message! 而父进程则从管道中读出来自于...
Client/Server通信(NamedPipe实现),C++实现,可以直接运行。
学习利用管道机制实现进程间的通信,加深对通信机制的理解。 实验内容: 1、 了解系统调用pipe()的功能和实际原理 2、 编写一段程序,使用管道实现父子进程之间的通信 a) 使用系统调用fork()创建一个子进程 b) 子...
linux平台下实现进程间通信的一种:pipe。以字符串为例。
visual c++利用管道和线程实现进程间通信
使用系统调用pipe()建立一条管道线;两个子进程P1和P2分别向管道各写一句话: Message from Child l! Message from Child 2! 父进程从管道中读出来自于两个子进程的信息,显示在屏幕上。 要求父进程先接收子...
进程间通信之无名管道(pipe) 注意: 1 只能用于具有亲缘关系的进程之间的通信 2 SIGPIPE信号的处理 七种进程间通信方式: 一 无名管道( pipe ) 二 有名管道( fifo ) 三 共享内存 shared memory 四 信号 ...
C# NamedPipe 通信,管道通信。 目前还有些BUG ,但是用作程序间的数据通信,问题应该不大,建议用于 Json 通信。 做这玩意出来,起初想法是用作 winService 和 winform 的通信,可以通过winfrom 上的操作,来控制...
VC 多任务通信 pipe
1 管道通信 利用pipe 和lockf 系统调用 编写程序 实现同族进程间的通信 使用系统调用pipe 建立一条管道线;创建子进程P1 P2 … 子进程Pi分别向管道各写信息 而父进程则从管道中读出来自于各子进程的信息 实现进程...
1) 编制实现软中断通信的程序 使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上发出的中断信号(即按delete键),当父进程接收到这两个软中断的某一个后,父进程用系统调用kill()向两...
实现进程间通信,编译通过,可以运行!更利于理解操作系统原理
多个进程利用pipe连接构成pipe ring, 利用这个环进行pipe sorting
利用pipe进程间通信时,到底通信的是什么?此软件可以读取namedpipe的内容
一些小程序 大家支持一下 下载后评论 既不浪费 又是支持~~~
匿名管道通信从而实现进程间1G大文件的传输。
在Linux系统中使用系统调用fork()创建两个子进程,使用系统调用pipe()建立一个管道,两个子进程分别向管道各写一句话: Child process 1 is sending a message! Child process 2 is sending a message! 而父进程则...
FIFO配合管道PIPE,实现FIFO传递数据,PIPE触发epoll,异步IO效果