Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android平板电脑上的Websocket服务器即服务_Java_Android_Service_Websocket_Server - Fatal编程技术网

Java Android平板电脑上的Websocket服务器即服务

Java Android平板电脑上的Websocket服务器即服务,java,android,service,websocket,server,Java,Android,Service,Websocket,Server,我有下面的场景。 Websocket服务器,作为后台服务在android平板电脑上运行。 在服务器启动以处理应用程序内通信后,此服务还会启动Websocket客户端。我在一个单独的项目中还有一个JavaWebSocket客户端,用于连接服务器并与服务器通信 只要我不尝试将WebSocket服务器作为服务实现,这一切都可以正常工作。 我可以连接并发送数据,就像没有明天一样 但一旦我将Websocket服务器作为服务运行,我就无法再连接到它。既不使用我的“InApp客户端”,也不使用我的独立java

我有下面的场景。 Websocket服务器,作为后台服务在android平板电脑上运行。 在服务器启动以处理应用程序内通信后,此服务还会启动Websocket客户端。我在一个单独的项目中还有一个JavaWebSocket客户端,用于连接服务器并与服务器通信

只要我不尝试将WebSocket服务器作为服务实现,这一切都可以正常工作。 我可以连接并发送数据,就像没有明天一样

但一旦我将Websocket服务器作为服务运行,我就无法再连接到它。既不使用我的“InApp客户端”,也不使用我的独立java客户端

我在清单中设置了internet的权限,并尝试了不同的端口。我还使用adb转发tcp:38301 tcp:38301执行端口转发

尝试使用应用内客户端连接到服务器时出现以下错误: E/WSCLient﹕ 连接到/127.0.0.1(端口38301)时出错:连接失败:ECONREFUSE(连接被拒绝)

我还尝试使用localhost而不是127.0.0.1

这是我的服务器的源代码:

private class WSServer extends WebSocketServer{
        private static final String TAG = "WSServer";
//      private static final int TIMEOUT = 60;
        private int port = 38301;
        private int testLevel = Constants.BOUNCE_MESSAGES;

        public WSServer( InetSocketAddress address ) {
            super( address );
            Log.d(TAG, "creating instance of local WSServer");
            wsClient = new WSClient(port);
        }

        public WSServer(int port)
        {
            super( new InetSocketAddress( port ) );
            Log.d(TAG, "creating instance of local WSServer on port " + port);
            wsClient = new WSClient(port);
        }

        @Override
        public void onOpen(WebSocket conn, ClientHandshake handshake) {
            Log.d(TAG, "connection Details: " + conn.getLocalSocketAddress() + "; " + conn.getRemoteSocketAddress());
            Log.d(TAG, "Connection successfully opened on port: " + this.getPort());
        }

        @Override
        public void onClose(WebSocket conn, int code, String reason, boolean remote) {
            Log.d(TAG, "Connection closed");
        }

        @Override
        public void onMessage(WebSocket conn, String message) {
            Log.d(TAG, "Received Message: " + message);
            if(testLevel == Constants.BOUNCE_MESSAGES)
            {
                conn.send(new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()) + ": " + message);
            }
        }

//      @Override
//      public void onFragment( WebSocket conn, Framedata fragment ) {
//          System.out.println( "received fragment: " + fragment );
//      }

        @Override
        public void onError(WebSocket conn, Exception ex) {
            Log.d(TAG, "onError()", ex);
        }
}
这是我的服务的源代码:

public CommManager(){
        wsServer = new WSServer(port);
        Log.d(TAG, "Server Adress: " + wsServer.getAddress());
    }

    public CommManager(int port){
        this.port = port;
        Log.d(TAG, "CommManager is starting");
        wsServer = new WSServer(port);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle extras = intent.getExtras();
        if(extras == null) {
            Log.d(TAG, "Port wurde nicht gesetzt. Default Port " + port + " wird verwendet");
        }
        else {
            port = (Integer)extras.get("port");
        }

        return startMode;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "Binding Service to Activity");
        return commManagerBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {

        return allowRebind;
    }
最后但并非最不重要的是,我的应用程序内客户端:

private class WSClient {

        public String message;
        private static final String TAG = "WSCLient";

        private WebSocketClient mWebSocketClient;
        private int port = 38301;

        public WSClient(){
            connectWebSocket();
        }
        public WSClient(int port) {
            Log.d(TAG, "starting local WSClient");
            if(port != 0){
                this.port = port;
            }
            connectWebSocket();
        }

        private void connectWebSocket() {
            Log.d(TAG, "establishing Connection to local WSServer");
            URI uri;
            try {
                uri = new URI("ws://localhost:" + port);
            } catch (URISyntaxException e) {
                Log.e(TAG, "uri-error: " + "ws://localhost:" + port);
                e.printStackTrace();
                return;
            }

            mWebSocketClient = new WebSocketClient(uri) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                    Log.d(TAG, "Opened");
                    mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
                }

                @Override
                public void onMessage(String s) {
                    Log.d(TAG, "received Message: " + s);
                }

                @Override
                public void onClose(int i, String s, boolean b) {
                    Log.d(TAG, "Closed " + s);
                }

                @Override
                public void onError(Exception e) {
                    Log.e(TAG, "Error " + e.getMessage());
                }
            };
            mWebSocketClient.connect();
        }

        public void sendMessage(String message) {

            if(mWebSocketClient != null) {
                mWebSocketClient.send(message);
            }
            else {
                Log.e(TAG, "No WebSocketClient available. Trying to reconnect and create new Client instance");
                connectWebSocket();
            }
        }

    }
我不知道为什么我不能与我的服务器建立连接。
希望您能提供帮助。

所以我通过改变实现方式解决了这个问题。我对服务器和客户机类进行了子类化,然后它就开始工作了。:)

我也在试着做同样的事情。我可以问一下WebSocketServer类来自哪个库吗?我正在使用以下库:你能分享你的完整代码吗?我正在实现类似的服务。很抱歉,我不再有权访问该代码,因为我更改了employee@Chao如果您仍然在寻找一些代码,那么下面有一些详细信息。