如何在Java Mac OS X中正确指定datagramSocket的端口

时间:2012-03-18 22:29:36

标签: java udp p2p datagram

我有以下问题。

我正在尝试使用UDP协议来实现P2P系统来传输和接收文件。我的主要问题是沟通。当我尝试建立对等体的接收机制时,它会引发以下异常:

java.net.BindException: Address already in use

虽然我使用此命令(在Mac OS X下)调查了我使用的端口:

sudo lsof -Pn | grep "insert port number here"

我意识到 所有 我调用的端口是完全免费的。

这是我不稳定的代码:

服务器创建:

   while(!serverReady){
            this.appOutputObj.serverInitializationFailed();
            boolean correctData = this.requestingData();

            //ERROR: In this if condition!
            if(correctData){
            serverReady = this.appEngine.initializeEngineServer();

            System.out.println(serverReady);

            if(serverReady)
                break;
            }
        }


    public boolean initializeServer(){


        try {

            this.residentServer = new Server(5001, 5002, this.mcAddress, this.dataPort, this.controlPort);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
   }

public Server(int dataPort, int controlPort, InetAddress newMcAddress, int mcDataPort, int mcControlPort) throws SocketException{
    this.dataPort = dataPort;
    this.controlPort = controlPort;

    this.dataDatagramSocket = new DatagramSocket(this.dataPort);            //<-- Line with exception raising.
    this.controlDatagramSocket = new DatagramSocket(this.controlPort);

    this.mcAddress = newMcAddress;
    this.mcDataPort = mcDataPort;

    this.dataMST = new MulticastServerThread(this.dataPort, this.mcAddress, this.mcDataPort);
    this.controlMST = new MulticastServerThread(this.controlPort, this.mcAddress, this.mcControlPort);

    this.dataMST.start();
    this.controlMST.start();
    }

对此有何解决方案?

0 个答案:

没有答案