简体   繁体   中英

Android ServerSocket accept permission denied

I was writing a sample android app which will create a simple http server, but faced a little problem.

Here's my 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();
            }
        });
    }
}

And here's the SocketProcessor class

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

The problem is it's giving me an exception saying that permission denied, although I do have the permission in my Manifest

<?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>

What's wrong with the permission, am I missing something?

Thanks in advance.

Here is the stacktrace of my exception

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

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

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

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

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