0

私は今、この現在の問題に直面しています。

コマンドをデバイスに送信し、Android エミュレーターからソケットへのデバイスからの応答を受信できます。

しかし、同じアプリケーションをタブレットにインストールすると、問題が発生します。デバイスが接続されているかどうかのステータスを確認するコマンドを初めて送信すると、デバイスが接続されているという応答が送信されますが、2回目にコマンドを送信すると、次の例外がスローされます。

java.net.ConnectException: /192.168.1.106:8002 - 接続が拒否されました。

これはリクエストを行うコードです:

public static String sendRequestandResponse(final String host,final int  port,
            final String command,
            final int timeoutInMillis,final int responseLength) throws UnknownHostException,NetworkSettingException

            {
        if (host == null)
        {
            throw new NullPointerException("host is null");  //NOPMD
        }
        Socket clientSocket=null;
        try {

            /**
             * Creating socket connection with IP address and port number to send Command
             */
            try{
                clientSocket = new Socket();

                SocketAddress remoteAdr = new InetSocketAddress(host, port);
                clientSocket.connect(remoteAdr, 1000);
                clientSocket.setSoTimeout(timeoutInMillis);
            }catch (Exception e) {
                e.printStackTrace();
                throw new NetworkSettingException(e.getMessage());
            }




            final PrintWriter outPutStream = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), CHARSET));
            try
            {
                outPutStream.print(command);
                        outPutStream.flush();
                    BufferedReader responseString = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), CHARSET));
                response   = new StringBuilder(); 
                try
                {

                    int pos = 0;
                    while (true)
                    {
                        pos++;
                        System.out.println(pos);
                        int i=responseString.read();
                        byte[] resp={(byte)i};
                        System.out.println(new String(resp));
                        response.append(new String(resp));
                        if(pos>=responseLength){
                            {
                                clientSocket.shutdownInput();
                                clientSocket.shutdownOutput();
                                clientSocket.close();
                                Log.d("ConnectionSocket", "Socket closed with break");
                                break;
                            }


                        }
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                }
                finally
                {
                    responseString.close();
                }


            }
            finally
            {
                outPutStream.close();
            }

        }

        catch(IOException ex){
        }
        catch(NullPointerException ex){  //NOPMD
        }

        finally
        {
            try {
                clientSocket.shutdownInput();
                clientSocket.shutdownOutput();
                clientSocket.close();
            } catch (NullPointerException ex) { //NOPMD
            } catch (IOException ex) {
            }
        }
        return response.toString();
            }

最初はソケットを閉じないと思うので、2回目は接続を拒否します。

ただし、エミュレーターでも同じコードが機能します。

4

1 に答える 1

0

サーバーが接続を受け入れていない場合にのみ、接続が拒否されます。

ほとんどの場合、問題は、信頼されていない着信接続をブロックするファイアウォールです。

サーバーにそれをブロックするファイアウォールがある場合、私のアプリケーションは主にこのメッセージを表示します。

したがって、アプリケーションのファイアウォールに例外リストを追加できます。

于 2012-02-08T08:29:48.227 に答える