单独线程中的ServerSocket抛出java.net.BindException:绑定失败:Android

时间:2016-03-14 21:37:58

标签: java android multithreading

我计划制作一个应用程序,通过本地WiFi连接将文件从一部手机传输到另一部手机。我面临的问题是,与桌面PC上的Java不同,我在尝试将端口绑定到新的SocketServer实例时会在Android上获得权限问题。

我正在人们建议的单独线程中运行ServerSocket accept()方法,并且我在AndroidManifest.xml中拥有正确的权限。

这是我的代码:

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

}

服务器

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>

这可能是什么问题?这是我的例外

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

1 个答案:

答案 0 :(得分:4)

在基于Linux的系统上,1024以下的端口只能由具有超级用户权限的应用程序打开。请选择更高的端口。