如何将WebSocket服务器连接到默认端口号80

时间:2014-11-10 16:05:03

标签: android websocket

当我尝试通过6001端口连接成功连接WebSocketServer时,但是当我尝试连接到默认端口时,它会出现“绑定失败:EACCES(权限被拒绝)”。

它正确显示端口号80,但它直接调用onError函数,这是我的代码

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.local_gattip);
        ctx = this;
        lActivity = this;
        mWebview = (WebView) findViewById(R.id.webview);
        task = new StartServerTask();
        try {
            // Async task to start web socket server
            task = new StartServerTask();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "");
            else
                task.execute("");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        mWebview.loadUrl("file:///android_asset/www/index.html");
    }   

    private class StartServerTask extends AsyncTask<String, Object, String> {
        @Override
        protected String doInBackground(String... params) {
            WebSocketServer server = new MYServer(new InetSocketAddress("localhost", 6001), ctx);               
                server.start();         
            // start the server after getting the WebSocketServer reference
            return "";
        }
    }   

    private class StartServerTaskAgain extends AsyncTask<String, Object, String> {
        @Override
        protected String doInBackground(String... params) {
                try {
                    server = new MYServer(ctx);
                           server.start();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            return "";
        }
    }
    }

    public class MYServer extends WebSocketServer  {

        private WebSocket webSocket;
        public MYServer(InetSocketAddress address, Context ctx) {       
            super(address);
        }   

        public MYServer(Context ctx) throws UnknownHostException {
            super();
        }   

        @Override
        public void onOpen(WebSocket conn, ClientHandshake handShake) {
             Log.v("MYServer","connection to " + conn.getRemoteSocketAddress());        
        }
    }

由于

0 个答案:

没有答案