我在编写一个JXTA的例程时出现这个问题:
<FATAL 16:36:50,669 IncomingUnicastServer:149>[1]Cannot bind ServerSocket on /0.0.0.0:9701
java.net.BindException:Address already in use:JVM_Bind
……
请问是怎么回事,是否又缺少什么包?

解决方案 »

  1.   

    一个进程使用的端口不能是另一个进程占用的端口,只不过这里的进程都是java程序而已,要解决就要看看端口占用问题程序1一直监听比如8080 端口,那么程序2就不能使用8080发送数据,但是应该发送数据到8080端口
      

  2.   

    呵呵
    居然在这里会出现JXTA的...其实很简单JXTA技术手册上也有说过
    当你需要在本机启动两个对等体的时候.想他们可以通讯的话.在配置哪里必须使用不同的端口..而且这个两个对等体最好分别放在不同的目录里面..否则效果不明显的..
    因为大家共用一个目录的话..等于自己建立的通告.自己来发现.没有意义了..呵呵..说多了..有点离题啦..改端口的话..将.jxta那个文件夹删除了就可以了.会再出现配置的框的
      

  3.   

    tttick是高手啊!写得很明白。配置端口我己经会了,不会再发生占用端口现象,但现在的问题是程序RestoPeer创建的对等体,另一个程序HungryPeer却不能发现,两个程序在不同的目录,端口也改动了,而且HungryPeer可以发现RestoPeer创建的对等组!!!
      

  4.   

    但现在的问题是程序RestoPeer创建的对等体,另一个程序HungryPeer却不能发现,两个程序在不同的目录,端口也改动了,而且HungryPeer可以发现RestoPeer创建的对等组!!!
    不明白你上面说的是什么意思.具体点.和操作系统没有关系.因为是JAVA嘛.呵呵.不过和防火墙有点关系.有时会出现穿越不了防火墙的问题.不过我的程序后来解决了.这是后话.如果你不会穿越防火墙的话.就先将防火墙关了再测试.如果程序没有问题的话.一般都能发现对等组的.
      

  5.   

    我己关闭了防火墙,还是一样可以发现对等组但是定位不了对等组里面的对等体!!这是一个P2P书籍的例子程序,书名《JXTA技术手册》即《JXTA in a Nutshell,是第五章的实例,RestoPeer程序创建了RestoNet对等组和RestoPeer通告和InputPipe,并且加入到了RestoNet对等组中,然后等待;而HungryPeer程序发现RestoNet对等组,并创建一个OutPipe并加入到RestoNet对等组,然后利用InputPipe和OutPipe互相通讯。我执行的问题是:HungryPeer程序可以发现RestoNet对等组并可以加入,但定位(locating)RestoPeer失败,不能通讯。因为是刚开始学习Java和JXTA,所以请多指教!!源程序在http://examples.oreilly.com/jxtaian/,里面第五章的RestoPeer部分和HungryPeer部分。
      

  6.   

    //这是HungryPeer.java程序的第一部分
    import java.io.*;
    import java.util.Enumeration;
    import java.util.Vector;
    import net.jxta.peergroup.PeerGroup;
    import net.jxta.peergroup.PeerGroupFactory;
    import net.jxta.exception.PeerGroupException;
    import net.jxta.document.AdvertisementFactory;
    import net.jxta.document.Advertisement;
    import net.jxta.document.StructuredDocument;
    import net.jxta.document.Element;
    import net.jxta.document.StructuredDocumentFactory;
    import net.jxta.document.MimeMediaType;
    import net.jxta.discovery.DiscoveryService;
    import net.jxta.pipe.PipeService;
    import net.jxta.pipe.InputPipe;
    import net.jxta.pipe.PipeID;
    import net.jxta.pipe.OutputPipe;
    import net.jxta.endpoint.Message;
    import net.jxta.protocol.PipeAdvertisement;
    import net.jxta.protocol.PeerGroupAdvertisement;
    import net.jxta.id.IDFactory;
    // The HungryPeer joins the RestoNet PeerGroup and searches for
    // RestoPeers. The HungryPeer then establishes a pipe connection to
    // all the RestoPeers that it discovered. The HungryPeer sends
    // auction requests for French fries to RestoPeers and then waits for
    // auction bids from RestoPeerspublic class HungryPeer {    private PeerGroup netpg = null;     // NetPeergroup
        private PeerGroup restoNet = null;  // Resto Peergroup    // Services within the RestoNet Peergroup
        private DiscoveryService disco;     // Discovery Service
        private PipeService pipes;          // Pipe Service
        private PipeAdvertisement myAdv;    // Hungry peer pipe advertisement
        private InputPipe myPipe;           // Input pipe to talk to hungry peer
        private MimeMediaType mimeType = new MimeMediaType("text", "xml");    private int timeout = 3000;         // Discovery timeout
        private int rtimeout = 30000;       // Pipe Resolver Timeout
        // All RestoPeers found
        private Vector restoPeerAdvs = new Vector();
        private Vector restoPeerPipes = new Vector();    private String myIdentity = "Bill Joy";  // Identity of this HungryPeer
        private String friesRequest ="medium";   // Fries Auction request    public static void main(String args[]) {
            HungryPeer myapp = new HungryPeer();
            myapp.startJxta();
            System.exit(0);
        }    private void startJxta() {
            try {
                // Discover (or create) and join the default jxta NetPeerGroup
                netpg = PeerGroupFactory.newNetPeerGroup();
            } catch (PeerGroupException e) {
                //Couldn't initialize; can't continue
                System.out.println("Fatal error : creating the NetPeerGroup");
                System.exit(1);
            }        // Discover and join the RestoNet Peergroup
            try {
                if (!joinRestoNet()) {
                    System.out.println("Sorry could not find the RestoNet Peergroup");
                    System.exit(2);
                }
            } catch (Exception e) {
                System.out.println("Can't join RestoNet group");
                System.exit(1);
            }        // Set our HungryPeer communication pipe so RestoPeers
            // can talk to us
            if (!setHungryPeerPipe()) {
                System.out.println(
                    "Aborting due to failure to create our HungryPeer pipe");
                System.exit(1);
            }        // Attempt to locate RestoPeers in RestoNet
            discoverRestoPeers();        // Connect to RestoPeers that have been discovered
            connectToRestoPeers();        // I am hungry. Send an auction request for French Fries
            // to the connected RestoPeers.
            sendFriesAuctionRequests();        //Process incoming bids from RestoPeers
            receiveFriesBids();
        }    // This method is used to discover the RestoNet Peergroup.
        // If found the peer will join the peergroup
        private boolean joinRestoNet() {        int count = 3; // maximum number of attempts to discover        System.out.println("Attempting to discover the RestoNet Peergroup");        // Get the Discovery service handle from the NetPeerGroup
            DiscoveryService hdisco = netpg.getDiscoveryService();        // All discovered RestoNet Peers
            Enumeration ae = null;        // Loop until we find the "RestoNet" Peergroup advertisement
            // or we've exhausted the desired number of attempts
            while (count-- > 0) {
                try {
                    // Check if we have the advertisement in the local
                    // peer cache
                    ae = hdisco.getLocalAdvertisements(DiscoveryService.GROUP,
                                                "Name", "RestoNet");                // If we found the RestoNet advertisement, we are done
                    if ((ae != null) && ae.hasMoreElements())
                        break;                // The RestoNet advertisement is not in the local
                    // cache . Send a discovery request to search for it.
                    hdisco.getRemoteAdvertisements(null,
                               DiscoveryService.GROUP, "Name", "RestoNet", 1, null);                // Wait to give peers a chance to respond
                    try {
                        Thread.sleep(timeout);
                    } catch (InterruptedException ie) {}
                } catch (IOException e) {
                    // Found nothing! Move on.
                }
            }        // Check if we found the RestoNet advertisement
            if (ae == null || !ae.hasMoreElements()) {
                return false;
            }        System.out.println("Found the RestoNet PeerGroup Advertisement");
            // Get the advertisement
            PeerGroupAdvertisement adv =
                (PeerGroupAdvertisement) ae.nextElement();        try {
                // Call the PeerGroup Factory to instantiate a new
                // peergroup instance
                restoNet = netpg.newGroup(adv);            // Get the Discovery and Pipe services to
                // be used within the RestoNet Peergroup
                disco = restoNet.getDiscoveryService();
                pipes = restoNet.getPipeService();
            } catch (Exception e) {
              System.out.println("Could not create RestoPeerGroup");
              return false;
            }        System.out.println("The HungryPeer joined the restoNet PeerGroup");
            return true;
        }    // Create the HungryPeer pipe to receive bid responses
        // from RestoPeers. The advertisement of this pipe is sent as part
        // of the auction request for RestoPeers to respond.
        private boolean setHungryPeerPipe() {
            try {
                // Create a pipe advertisement for our hungry peer. This
                // pipe will be used within the RestoNet peergroup for other
                // peers to talk to our hungry peer
                myAdv = (PipeAdvertisement)
                    AdvertisementFactory.newAdvertisement(
                    PipeAdvertisement.getAdvertisementType());            // Initialize the advertisement with unique peer information
                // So we can communicate
                myAdv.setPipeID(IDFactory.newPipeID(restoNet.getPeerGroupID()));
                myAdv.setName("restoNet:HungryPipe:" + myIdentity);            // Set the pipe type to be unicast unidrectional
                myAdv.setType(PipeService.UnicastType);            // Create the input pipe
                myPipe = pipes.createInputPipe(myAdv);
            } catch (Exception e) {
                System.out.println("Could not create the HungryPeer pipe");
                return false;
            }
            return true;
        }
      

  7.   

    //这是HungryPeer.java的第二部分   
       // Discover RestoPeers that have joined RestoNet.
        // RestoPeers are discovered via their published pipe advertisement.
        private void discoverRestoPeers() {
            int found = 0;           // Count of RestoPeers found
            int count = 10;          // Discovery retries        System.out.println("Locating RestoPeers in the RestoNet Peergroup");        // Try to find at least two RestoPeers (an arbitrary number)
            // RestoPeers are found by their pipe advertisements
            while (count-- >0) {
                try {
                    // Check if we already have restaurant advertisements
                    // in our local peer cache
                    Enumeration ae =
                        disco.getLocalAdvertisements(DiscoveryService.ADV,
                                         "name", "RestoNet:RestoPipe:*");                // If we found some advertisements in the cache,
                    // add them to the list
                    if (ae != null && ae.hasMoreElements()) {
                        // Reset count and RestoPeerAdvs as we have
                        // just retrieved all advertisements, including
                        // previously cached ones
                        found = 0;
                        restoPeerAdvs.removeAllElements();
                        while (ae.hasMoreElements()) {
                            restoPeerAdvs.addElement(ae.nextElement());
                            ++found;
                        }
                        if (found > 1)
                            break;     // Want to find at least two
                    }                // Did not find enough advertisement in the cache.
                    // Send a remote discovery request to search for
                    // more RestoPeer advertisements
                    disco.getRemoteAdvertisements(null,
                                 DiscoveryService.ADV,
                                 "name", "RestoNet:RestoPipe:*", 5, null);                // Give the peers a chance to respond
                    try {
                        Thread.sleep(timeout);
                    } catch (InterruptedException e) {}
                } catch (IOException e){
                    // Found nothing! Move on
                }
            }
            // Completed RestoPeer Discovery
            System.out.println("Found " + found + " RestoPeers(s)");
        }    // Method to connect and open output pipes to all the
        // RestoPeers that we have discovered. Each RestoPeer is
        // identified by its unique RestoPeer pipe advertisement.
        private void connectToRestoPeers() {
            // Enumerate all the RestoPeer pipe advertisments we have discovered
            // and attempt to connect a pipe which each of them
            for (Enumeration en = restoPeerAdvs.elements();
                 en.hasMoreElements();) {            PipeAdvertisement padv = (PipeAdvertisement) en.nextElement();
                try {
                    System.out.println(
                        "Attempting to connect to discovered RestoPeer");                // Create an output pipe connection to the RestoPeer
                    OutputPipe pipeOut = pipes.createOutputPipe(padv,
                                                                rtimeout);                // Check if we have a connected pipe
                    if (pipeOut == null) {
                        // Failed; go to next RestoPeer
                        System.out.println(
                            "Failure to connect to RestoPeer Pipe:" +
                            padv.getName());
                        continue;
                    }                // Save the output pipe
                    restoPeerPipes.addElement(pipeOut);
                    System.out.println("Connected pipe to " + padv.getName());
                } catch (Exception e) {
                    // Error during connection go to next RestoPeer
                    System.out.println("RestoPeer may not be there anymore:" +
                                        padv.getName());
                     continue;
                }
            }
        }    // Send an auction request for French Fries to all the RestoPeer
        // pipes we have successfully connected
        private void sendFriesAuctionRequests() {
            // Enumerate all the RestoPeer pipe connections we have successfully
            // connected to
            for (Enumeration en = restoPeerPipes.elements();
                en.hasMoreElements();) {
                OutputPipe op = (OutputPipe) en.nextElement();
                try {
                    // Construct the request document
                    StructuredDocument request  =
                        StructuredDocumentFactory.newStructuredDocument(mimeType,
                        "RestoNet:Request");                // Fill up the Fries auction request argument
                    Element re;
                    re = request.createElement("Name", myIdentity);
                    request.appendChild(re);
                    re = request.createElement("Fries", friesRequest);
                    request.appendChild(re);                // Create the pipe message to send
                    Message msg = pipes.createMessage();                // Fill the first message element which is the HungryPeer
                    // pipe advertisement return address. We need this
                    // so RestoPeers can respond to us
                    msg.addElement(msg.newMessageElement("HungryPeerPipe",
                             mimeType, myAdv.getDocument(mimeType).getStream()));                // Fill the second message element, which is
                    // the fries request. Insert the document
                    // in the message
                    msg.addElement(msg.newMessageElement("Request",
                             mimeType, request.getStream()));                // Send the auction message to the RestoPeer
                    op.send(msg);
                    System.out.println("Sent Fries Auction Request ("
                        + friesRequest + ") to connected peers");
                } catch (Exception ex) {
                    // Error sending auction request
                    System.out.println(
                        "Failed to send auction request to RestoPeer");
                }
            }
        }
      

  8.   

    那书我也看过了..我开始就是从那书开始学的..但是哪里的程序对于现在这个版本的JXTA是行不通的.API已经改变了好多了.
    至于你说的要在对等组里找到其他的对等体,这个和JXTA没关.主要是你自己怎样用程序去实现.主要还是理解了JXTA的7个协议以及里面的具体机制,具体程序的实现还是需要自己去探索.去JXTA官方站点看看吧,很多很好的例子.SUN提供的关于JXTA的WIKI也不错的.
      

  9.   

    tttick的回答让我简直有点绝望!不过能否使用旧的API来运行通过这个程序呢?如果那样该怎么做?我安装了JDK1.3甚至是JDK1.2,还是不能运行通过!!这个问题实际上一步步地转化为了定位对等组中管道的通告的问题了!!
      

  10.   

    和JDK没有什么关系吧.
    是JXTA的包更新的快.
    那书上的好像还是1.1的
    现在都2.6了咯.我建议你还是别急着实现程序,先好好地理解了JXTA的机制先.
      

  11.   

    我从网上下载了JXTA1.0的库后,成功地运行了这个程序!!!看来懂JXTA的人还很少,非常感谢tttick的指点,这100分都给你了!