Android ServerSocket接受权限被拒绝

时间:2015-09-17 12:21:22

标签: java android serversocket

我正在编写一个示例Android应用程序,它将创建一个简单的http服务器,但面临一些问题。

这是我的MainActivity

public class MainActivity extends Activity {

    private ServerSocket ss;

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

    private void Init() {
        Button btnStart = (Button) findViewById(R.id.btn_start);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            if(ss != null){
                                return;
                            }

                            ss = new ServerSocket(8080);

                            while (true) {
                                Socket s = ss.accept();
                                System.err.println("Client accepted");
                                new Thread(new SocketProcessor(s)).start();
                            }
                        } catch (Throwable throwable) {
                            throwable.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
}

这是SocketProcessor类

public class SocketProcessor implements Runnable {

    private Socket s;
    private InputStream is;
    private OutputStream os;

    public SocketProcessor(Socket s) throws Throwable {
        this.s = s;
        this.is = s.getInputStream();
        this.os = s.getOutputStream();
    }

    public void run() {
        try {
            readInputHeaders();
            writeResponse("<html><body><h1>Hello world!</h1></body></html>");
        } catch (Throwable t) {
                /*do nothing*/
        } finally {
            try {
                s.close();
            } catch (Throwable t) {
                    /*do nothing*/
            }
        }
        System.err.println("Client processing finished");
    }

    private void writeResponse(String s) throws Throwable {
        String response = "HTTP/1.1 200 OK\r\n" +
                "Server: YarServer/2009-09-09\r\n" +
                "Content-Type: text/html\r\n" +
                "Content-Length: " + s.length() + "\r\n" +
                "Connection: close\r\n\r\n";
        String result = response + s;
        os.write(result.getBytes());
        os.flush();
    }

    private void readInputHeaders() throws Throwable {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        while (true) {
            String s = br.readLine();
            if (s == null || s.trim().length() == 0) {
                break;
            }
        }
    }
}

问题在于它给了我一个例外,说明了许可被拒绝,尽管我在我的清单中有权限

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

    <uses-permission android:name="ANDROID.PERMISSION.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

许可有什么问题,我错过了什么吗?

提前致谢。

这是我的异常的堆栈跟踪

09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ java.net.SocketException: socket failed: EACCES (Permission denied)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:660)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at java.net.PlainServerSocketImpl.create(PlainServerSocketImpl.java:38)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at java.net.ServerSocket.<init>(ServerSocket.java:103)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at java.net.ServerSocket.<init>(ServerSocket.java:74)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at com.mainserver.myapplication.MainActivity$1$1.run(MainActivity.java:37)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at libcore.io.Posix.socket(Native Method)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:645)
09-17 17:01:11.072  12667-12842/com.mainserver.myapplication W/System.err﹕ ... 6 more

2 个答案:

答案 0 :(得分:7)

您声明的权限需要替换为:

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

答案 1 :(得分:-1)

我认为您应该为套接字创建一个新类,因为现在关闭的唯一方法是关闭应用程序本身,也许它应该在异步任务中运行。