服务端:
package primecruncher;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.endpoint.MessageElement;
import net.jxta.discovery.DiscoveryService;
import net.jxta.pipe.PipeID;
import net.jxta.exception.PeerGroupException;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.document.*;
import net.jxta.id.IDFactory;
import net.jxta.pipe.PipeService;
import net.jxta.endpoint.Message;
import java.io.FileInputStream;
import net.jxta.util.JxtaServerPipe;
import net.jxta.util.JxtaBiDiPipe;
import net.jxta.endpoint.StringMessageElement;
import java.io.IOException;
import java.io.FileOutputStream;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.pipe.PipeMsgEvent;
public class PrimePeer implements PipeMsgListener {
private PipeAdvertisement pipeAdv;
private JxtaServerPipe serverPipe;
private final static String SenderMessage = "pipe_tutorial";
private static PeerGroup group;
private static DiscoveryService discoSvc;
private static final String PIPE_ADV_FILE = "primeserver_pipe.adv";
private final static String ReturnCred = "Authentication credential ";
private final static String peerGroupID = "peer group ID ";
private final static String peerID = "peer ID ";
private JxtaBiDiPipe bipipe;
private String xiaoxi;
private PipeService pipeSvc;
public static void main(String[] args) {
PrimePeer pp = new PrimePeer();
pp.startJxta();
pp.doAdvertise();
}
public PrimePeer() {
}
//初始化
private void startJxta()
{
try {
group = PeerGroupFactory.newNetPeerGroup();//获取GROUP引用
discoSvc = group.getDiscoveryService();
pipeSvc = group.getPipeService();
} catch (PeerGroupException e) {
System.out.println("Cannot create Net Peer Group: " + e.getMessage(
));
System.exit(-1);
}
}
//创建广告并发布
private void doAdvertise() {
try {
FileInputStream is = new FileInputStream(PIPE_ADV_FILE);
pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
is.close();
System.out.println("ok here");
//discoSvc.publish( pipeAdv, DiscoveryService.ADV);
System.out.println("ok here too");
// we want to block until a connection is established
} catch (Exception e) {
pipeAdv = (PipeAdvertisement)AdvertisementFactory. newAdvertisement(
PipeAdvertisement.getAdvertisementType());
PipeID pid = IDFactory.newPipeID(group.getPeerGroupID());
pipeAdv.setName("serve");
pipeAdv.setPipeID(pid);
//save pipeAd in file
Document pipeAdDoc = pipeAdv.getDocument(new MimeMediaType ("text/xml"));
try {
FileOutputStream os = new FileOutputStream(PIPE_ADV_FILE);
pipeAdDoc.sendToStream(os);
os.flush();
os.close();
System.out.println("Wrote pipe advertisement to disk.");
} catch (IOException ex) {
System.out.println("Can't save pipe advertisement to file " +
PIPE_ADV_FILE);
System.exit(-1);
System.out.println("failed to read/parse pipe advertisement");
e.printStackTrace();
System.exit(-1);
}
}
try {
discoSvc.remotePublish(pipeAdv, DiscoveryService.ADV);
System.out.println("Published module spec adv");
} catch (Exception e) {
System.out.println("Trouble publishing module spec adv: " +
e.getMessage());
}
//等待连接
while(true){
System.out.println("starting ServerPipe");
try {
serverPipe = new JxtaServerPipe(group, pipeAdv);
serverPipe.setPipeTimeout(0);
System.out.println("starting");
bipipe = serverPipe.accept();
System.out.println("starting1 ");
if (bipipe != null ) {
System.out.println("BiDi Pipe created");
receiveAndSendTestMessage(bipipe);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("not created");
return;
}
}
}
//发消息
private void receiveAndSendTestMessage (JxtaBiDiPipe pipe) {
try {
Message msg = new Message();
StringMessageElement sme = new StringMessageElement(SenderMessage, ReturnCred, null);
msg.addMessageElement(null, sme);
StringMessageElement sme1 = new StringMessageElement(peerGroupID, group.getPeerGroupID().toString(), null);
msg.addMessageElement(null, sme1);
StringMessageElement sme2 = new StringMessageElement(peerID, group.getPeerID().toString(), null);
msg.addMessageElement(null, sme2);
System.out.println("Sending back 'Hello World'");
pipe.sendMessage(msg);
}
catch (Exception ie) {
ie.printStackTrace();
}
}
//接收消息
public void pipeMsgEvent(PipeMsgEvent ev) {
Message msg = ev.getMessage();
MessageElement msgElement = msg.getMessageElement(null, SenderMessage);
xiaoxi=msgElement.toString();
System.out.println(xiaoxi);
// Get message
if (msgElement.toString() == null) {
System.out.println("null msg received");
}
}
}
客户端代码:
package primecruncher;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.util.JxtaBiDiPipe;
import net.jxta.discovery.DiscoveryService;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.pipe.PipeService;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.exception.PeerGroupException;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.pipe.PipeMsgEvent;
import net.jxta.rendezvous.RendezVousService;
import net.jxta.endpoint.Message;
import java.util.*;
import net.jxta.endpoint.MessageElement;
public class PrimeClient implements DiscoveryListener,PipeMsgListener{
private static PeerGroup group;
private Message msg,msg1;
private static DiscoveryService discoSvc;
private static PipeService pipeSvc;
String b;
String xiaoxi;
PipeAdvertisement pipeAdvert;
private RendezVousService rdv;
public PrimeClient(){}
public static void main(String[] args)
{
PrimeClient cl = new PrimeClient();
try{
cl.startJxta();
cl.doDiscovery();
System.out.println("wuyichang");
}catch(NullPointerException ee)
{ System.out.println("yichang");}
}
//初始化
private void startJxta() {
try {
group = PeerGroupFactory.newNetPeerGroup();
discoSvc = group.getDiscoveryService();
System.out.println("startJxta");
pipeSvc = group.getPipeService();
} catch (PeerGroupException e) {
System.out.println("Can't create net peer group: " +
e.getMessage());
System.exit(-1);
}
}
//远程搜索
private void doDiscovery()
{ discoSvc.addDiscoveryListener(this);
int peerQueryID = discoSvc.getRemoteAdvertisements(null, DiscoveryService.ADV,
null, null, 5);
System.out.println("sousuo");
}
//监听搜索结果并作出处理
public void discoveryEvent(DiscoveryEvent ev)
{ DiscoveryResponseMsg res = ev.getResponse();
Enumeration er = res.getAdvertisements();
if (er != null ) {
while (er.hasMoreElements())
{
pipeAdvert = (PipeAdvertisement)er.nextElement();
System.out.println("faxianguanggao");
}
}
//建立连接发送消息
try{
JxtaBiDiPipe bidipipe = new JxtaBiDiPipe(group,pipeAdvert,20000,this ,true );
msg = new Message();
StringMessageElement sme = new StringMessageElement("SenderMessage", "ReturnCred", null);
msg.addMessageElement(null, sme);
StringMessageElement sme1 = new StringMessageElement("a", group.getPeerGroupID().toString(), null);
msg.addMessageElement(null, sme1);
StringMessageElement sme2 = new StringMessageElement("b", group.getPeerID().toString(), null);
msg.addMessageElement(null, sme2);
bidipipe.sendMessage(msg);
System.out.println("fasong");
}catch(Exception e)
{
e.printStackTrace();
System.out.println("not created");
}
}
//接收消息
public void pipeMsgEvent(PipeMsgEvent ev) {
msg1 = ev.getMessage();
MessageElement msgElement = msg1.getMessageElement(null, "SenderMessage");
xiaoxi=msgElement.toString();
System.out.println(xiaoxi);
if (msgElement.toString() == null) {
System.out.println("null msg received");
}
}
}
package primecruncher;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.endpoint.MessageElement;
import net.jxta.discovery.DiscoveryService;
import net.jxta.pipe.PipeID;
import net.jxta.exception.PeerGroupException;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.document.*;
import net.jxta.id.IDFactory;
import net.jxta.pipe.PipeService;
import net.jxta.endpoint.Message;
import java.io.FileInputStream;
import net.jxta.util.JxtaServerPipe;
import net.jxta.util.JxtaBiDiPipe;
import net.jxta.endpoint.StringMessageElement;
import java.io.IOException;
import java.io.FileOutputStream;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.pipe.PipeMsgEvent;
public class PrimePeer implements PipeMsgListener {
private PipeAdvertisement pipeAdv;
private JxtaServerPipe serverPipe;
private final static String SenderMessage = "pipe_tutorial";
private static PeerGroup group;
private static DiscoveryService discoSvc;
private static final String PIPE_ADV_FILE = "primeserver_pipe.adv";
private final static String ReturnCred = "Authentication credential ";
private final static String peerGroupID = "peer group ID ";
private final static String peerID = "peer ID ";
private JxtaBiDiPipe bipipe;
private String xiaoxi;
private PipeService pipeSvc;
public static void main(String[] args) {
PrimePeer pp = new PrimePeer();
pp.startJxta();
pp.doAdvertise();
}
public PrimePeer() {
}
//初始化
private void startJxta()
{
try {
group = PeerGroupFactory.newNetPeerGroup();//获取GROUP引用
discoSvc = group.getDiscoveryService();
pipeSvc = group.getPipeService();
} catch (PeerGroupException e) {
System.out.println("Cannot create Net Peer Group: " + e.getMessage(
));
System.exit(-1);
}
}
//创建广告并发布
private void doAdvertise() {
try {
FileInputStream is = new FileInputStream(PIPE_ADV_FILE);
pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
is.close();
System.out.println("ok here");
//discoSvc.publish( pipeAdv, DiscoveryService.ADV);
System.out.println("ok here too");
// we want to block until a connection is established
} catch (Exception e) {
pipeAdv = (PipeAdvertisement)AdvertisementFactory. newAdvertisement(
PipeAdvertisement.getAdvertisementType());
PipeID pid = IDFactory.newPipeID(group.getPeerGroupID());
pipeAdv.setName("serve");
pipeAdv.setPipeID(pid);
//save pipeAd in file
Document pipeAdDoc = pipeAdv.getDocument(new MimeMediaType ("text/xml"));
try {
FileOutputStream os = new FileOutputStream(PIPE_ADV_FILE);
pipeAdDoc.sendToStream(os);
os.flush();
os.close();
System.out.println("Wrote pipe advertisement to disk.");
} catch (IOException ex) {
System.out.println("Can't save pipe advertisement to file " +
PIPE_ADV_FILE);
System.exit(-1);
System.out.println("failed to read/parse pipe advertisement");
e.printStackTrace();
System.exit(-1);
}
}
try {
discoSvc.remotePublish(pipeAdv, DiscoveryService.ADV);
System.out.println("Published module spec adv");
} catch (Exception e) {
System.out.println("Trouble publishing module spec adv: " +
e.getMessage());
}
//等待连接
while(true){
System.out.println("starting ServerPipe");
try {
serverPipe = new JxtaServerPipe(group, pipeAdv);
serverPipe.setPipeTimeout(0);
System.out.println("starting");
bipipe = serverPipe.accept();
System.out.println("starting1 ");
if (bipipe != null ) {
System.out.println("BiDi Pipe created");
receiveAndSendTestMessage(bipipe);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("not created");
return;
}
}
}
//发消息
private void receiveAndSendTestMessage (JxtaBiDiPipe pipe) {
try {
Message msg = new Message();
StringMessageElement sme = new StringMessageElement(SenderMessage, ReturnCred, null);
msg.addMessageElement(null, sme);
StringMessageElement sme1 = new StringMessageElement(peerGroupID, group.getPeerGroupID().toString(), null);
msg.addMessageElement(null, sme1);
StringMessageElement sme2 = new StringMessageElement(peerID, group.getPeerID().toString(), null);
msg.addMessageElement(null, sme2);
System.out.println("Sending back 'Hello World'");
pipe.sendMessage(msg);
}
catch (Exception ie) {
ie.printStackTrace();
}
}
//接收消息
public void pipeMsgEvent(PipeMsgEvent ev) {
Message msg = ev.getMessage();
MessageElement msgElement = msg.getMessageElement(null, SenderMessage);
xiaoxi=msgElement.toString();
System.out.println(xiaoxi);
// Get message
if (msgElement.toString() == null) {
System.out.println("null msg received");
}
}
}
客户端代码:
package primecruncher;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.util.JxtaBiDiPipe;
import net.jxta.discovery.DiscoveryService;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.pipe.PipeService;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.exception.PeerGroupException;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.pipe.PipeMsgEvent;
import net.jxta.rendezvous.RendezVousService;
import net.jxta.endpoint.Message;
import java.util.*;
import net.jxta.endpoint.MessageElement;
public class PrimeClient implements DiscoveryListener,PipeMsgListener{
private static PeerGroup group;
private Message msg,msg1;
private static DiscoveryService discoSvc;
private static PipeService pipeSvc;
String b;
String xiaoxi;
PipeAdvertisement pipeAdvert;
private RendezVousService rdv;
public PrimeClient(){}
public static void main(String[] args)
{
PrimeClient cl = new PrimeClient();
try{
cl.startJxta();
cl.doDiscovery();
System.out.println("wuyichang");
}catch(NullPointerException ee)
{ System.out.println("yichang");}
}
//初始化
private void startJxta() {
try {
group = PeerGroupFactory.newNetPeerGroup();
discoSvc = group.getDiscoveryService();
System.out.println("startJxta");
pipeSvc = group.getPipeService();
} catch (PeerGroupException e) {
System.out.println("Can't create net peer group: " +
e.getMessage());
System.exit(-1);
}
}
//远程搜索
private void doDiscovery()
{ discoSvc.addDiscoveryListener(this);
int peerQueryID = discoSvc.getRemoteAdvertisements(null, DiscoveryService.ADV,
null, null, 5);
System.out.println("sousuo");
}
//监听搜索结果并作出处理
public void discoveryEvent(DiscoveryEvent ev)
{ DiscoveryResponseMsg res = ev.getResponse();
Enumeration er = res.getAdvertisements();
if (er != null ) {
while (er.hasMoreElements())
{
pipeAdvert = (PipeAdvertisement)er.nextElement();
System.out.println("faxianguanggao");
}
}
//建立连接发送消息
try{
JxtaBiDiPipe bidipipe = new JxtaBiDiPipe(group,pipeAdvert,20000,this ,true );
msg = new Message();
StringMessageElement sme = new StringMessageElement("SenderMessage", "ReturnCred", null);
msg.addMessageElement(null, sme);
StringMessageElement sme1 = new StringMessageElement("a", group.getPeerGroupID().toString(), null);
msg.addMessageElement(null, sme1);
StringMessageElement sme2 = new StringMessageElement("b", group.getPeerID().toString(), null);
msg.addMessageElement(null, sme2);
bidipipe.sendMessage(msg);
System.out.println("fasong");
}catch(Exception e)
{
e.printStackTrace();
System.out.println("not created");
}
}
//接收消息
public void pipeMsgEvent(PipeMsgEvent ev) {
msg1 = ev.getMessage();
MessageElement msgElement = msg1.getMessageElement(null, "SenderMessage");
xiaoxi=msgElement.toString();
System.out.println(xiaoxi);
if (msgElement.toString() == null) {
System.out.println("null msg received");
}
}
}
相关推荐
**基于JXTA的P2P即时通信系统设计** P2P(Peer-to-Peer)技术是一种分布式网络架构,它打破了传统的客户端/服务器(C/S)模式和浏览器/服务器(B/S)模式对中心服务器的依赖。在P2P网络中,每个节点既是服务的提供...
学习这些资源可以帮助你深入理解P2P通信的原理,掌握JXTA框架的使用,为构建自己的P2P应用打下坚实的基础。虽然提供的代码可能水平不高,但对于初学者来说,通过实践和学习,能够逐步提升编程和理解能力。
**JXTA(JavaXTend)** 是一个开源的P2P(Peer-to-Peer)框架,由Sun Microsystems开发,旨在提供一种灵活、分散的网络应用程序基础设施。它允许不同的设备和应用程序之间进行通信,共享资源和服务,而无需中心...
**精通JXTA与JXTAShell:探索P2P技术新境界** JXTA(Java eXtensible Networking ...通过阅读本书,读者可以掌握JXTA的相关知识,利用这一强大的工具开发出创新的P2P应用程序,为互联网的未来开辟新的可能。
JXTA平台为P2P网络应用提供了协议和一个可编程的平台,支持基于Java的应用程序接口(API),能够实现对等点之间的直接通信。 JXTA的核心构件包括对等点(peer)、对等组(peergroup)、服务(Service)、管道(Pipe...
这本书"精通JXTA"深入介绍了这一技术,为开发者提供了全面的指导。 ### JXTA简介 JXTA的诞生是为了应对互联网的扩展性问题,尤其是随着设备联网的增加,中心化的服务模式变得效率低下。JXTA提供了一个基于XML的...
JXTA(Java/XML Peer-to-Peer Technology Architecture)旨在使设备之间能够直接通信,无需中心服务器,从而实现资源共享和协作。 JXTA 2.2 版本的开发文档详细阐述了如何使用这个框架进行程序设计和开发。以下是...
JXTA(Java eXtensible Peer-to-Peer Technology Platform)是Oracle公司开发的一个开源、跨平台的P2P(Peer-to-Peer)框架,它为构建基于对等网络的应用程序提供了一组标准协议和API。JXTA 2.7是这个框架的一个版本...
Jxta应用程序设计是指使用Jxta技术设计应用程序,旨在实现基于P2P模式的即时通信软件。Jxta应用程序设计需要考虑到Jxta的特点,例如基于Java的API、抽象层和对等点管理。 知识点9:Peer接口设计 Peer接口设计是指...
在开发过程中,理解和熟练使用这些类对于构建基于JXTA的P2P应用程序至关重要。它们提供了基本的通信基础设施,使得开发者可以专注于应用逻辑,而不是底层的网络通信细节。不过,需要注意的是,P2P通信涉及到的网络...
JXTA 2.0协议,作为一项先进的分布式计算技术标准,旨在为互联网应用提供一种全新的、去中心化的网络通信机制。其核心价值在于允许任何设备在网络中以对等的方式进行通信与资源共享,无需依赖于中央服务器或网关,...
总之,JXTA为Java开发者提供了一套强大的工具,使他们能够构建出具有高效、分散、安全特性的P2P应用程序。无论是文件共享、分布式计算还是物联网应用,JXTA都能够提供灵活的支持。通过深入理解和实践,开发者可以...
**JXTA 开发包1** 是一组专为开发P2P(对等网络)应用程序设计的组件。这个开发包源自官方站点 **jxta.org** ,并经过了安全检查,确保无病毒,为开发者提供了安全可靠的开发环境。P2P技术是一种分布式计算模型,...
BeepCore Java为JXTA提供了底层通信基础,使得开发者可以构建高级的P2P应用。 **JXTA_JAR**文件通常包含JXTA的库和API,是开发基于JXTA的应用程序所必需的。这些JAR文件提供了接口和实现,涵盖了从设备发现、服务...
JXTA 是一个开放源代码的P2P(对等网络)平台,由Sun Microsystems在2001年推出,旨在提供一种分布式计算模型,允许网络上的设备之间进行通信和协作。这个平台支持多种服务,包括消息传递、资源发现、身份验证和加密...
总结来说,"jxta.rar_jxta_jxta cms"这个压缩包中的三个关键组件——"jxta.jar"、"cms.jar"和"jdom.jar",为构建基于JXTA的P2P系统提供了必要的工具和库。它们共同构建了一个强大且安全的P2P环境,使得开发者能够...
学习这个压缩包,开发者不仅可以理解JXTA的工作原理,还能通过实践加深对P2P通信的理解,为开发自己的P2P应用打下基础。尽管代码可能简单,但对于初学者而言,它是理解P2P通信和JXTA实现的宝贵资源。
表明这个压缩包可能包含了一个基于JXTA(Java eXtensible Networking Technology Architecture)平台实现的P2P(peer-to-peer)通信的“HelloWorld”程序,这是一个学习和理解JXTA基本概念的典型起点。 **JXTA简介*...
JXTA(Java Xtreme Programming Toolkit)是一个开源项目,由Sun Microsystems发起,旨在提供一个P2P(对等网络)平台,用于构建分布式应用程序。JXTA Shell是这个平台的一个重要组成部分,它为开发者和管理员提供了...