简体   繁体   中英

ServerSocket in seperate thread throwing java.net.BindException: bind failed: EACCES (Permission denied) on Android

I am planning to make an app that transfers files from one phone to another via a local WiFi connection. The problem i am facing is that unlike Java on a desktop PC, i am getting permission issues on Android when trying to bind a port a new SocketServer instance.

I am running the ServerSocket accept() method in a seperate thread as people have suggested and I have the correct permissions in my AndroidManifest.xml .

Here is my code:

MainActivity

package ga.ognjen.quicksend;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements Runnable {

    private static TextView textView;
    private Button button;
    private Thread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);
        thread = new Thread(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thread.start();
            }
        });
    }

    public static void setLabelText(String msg) {
        textView.setText(msg);
    }

    @Override
    public void run() {
        Server server = new Server();
        server.listen();
    }

}

Server

package ga.ognjen.quicksend;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    private Socket socket;
    private ServerSocket serverSocket;

    public void listen() {
        try {
            serverSocket = new ServerSocket(44);
            socket = serverSocket.accept();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public int getPort() {
        return socket.getPort();
    }
    public String getLocalAdress() {
        return socket.getLocalAddress().toString();
    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ga.ognjen.quicksend">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

What could be the problem here? Here is my exception

03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err: java.net.BindException: bind failed: EACCES (Permission denied)
03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err:     at libcore.io.IoBridge.bind(IoBridge.java:99)
03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err:     at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:132)
03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err:     at java.net.ServerSocket.<init>(ServerSocket.java:105)
03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err:     at java.net.ServerSocket.<init>(ServerSocket.java:74)
03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err:     at ga.ognjen.quicksend.Server.listen(Server.java:17)
03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err:     at ga.ognjen.quicksend.MainActivity.run(MainActivity.java:36)
03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err: Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
03-14 22:27:40.533 27672-27748/ga.ognjen.quicksend W/System.err:     at libcore.io.Posix.bind(Native Method)
03-14 22:27:40.537 27672-27748/ga.ognjen.quicksend W/System.err:     at libcore.io.ForwardingOs.bind(ForwardingOs.java:56)
03-14 22:27:40.537 27672-27748/ga.ognjen.quicksend W/System.err:     at libcore.io.IoBridge.bind(IoBridge.java:97)
03-14 22:27:40.539 27672-27748/ga.ognjen.quicksend W/System.err:    ... 6 more

On Linux-based systems, ports below 1024 can only be opened by apps with superuser privileges. Please choose a higher port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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