Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.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.net.ConnectException:/192.168.1.106:8002-连接被拒绝(Android异常)_Java_Android_Sockets_Socketexception - Fatal编程技术网

java.net.ConnectException:/192.168.1.106:8002-连接被拒绝(Android异常)

java.net.ConnectException:/192.168.1.106:8002-连接被拒绝(Android异常),java,android,sockets,socketexception,Java,Android,Sockets,Socketexception,我现在面临着这个问题 我能够向设备发送命令,并从android emulator接收设备到套接字的响应 但是,当我在平板电脑上安装相同的应用程序时,出现了一个问题。第一次发送命令检查设备是否已连接的状态时,它会向我发送设备已连接的响应,但第二次发送命令时,它会引发以下异常: java.net.ConnectException:/192.168.1.106:8002-连接被拒绝 这是执行请求的代码: public static String sendRequestandResponse(final

我现在面临着这个问题

我能够向设备发送命令,并从android emulator接收设备到套接字的响应

但是,当我在平板电脑上安装相同的应用程序时,出现了一个问题。第一次发送命令检查设备是否已连接的状态时,它会向我发送设备已连接的响应,但第二次发送命令时,它会引发以下异常:

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();
            }
我认为第一次它没有关闭插座,所以第二次它拒绝连接


同样的代码也适用于emulator。

只有当服务器不接受连接时,连接才会被拒绝

大多数情况下,问题是防火墙阻止了任何不可信的传入连接

我的应用程序通常在服务器有防火墙阻止它时显示此消息


因此,您可以在防火墙中为您的应用程序添加例外列表。

确保您的选项卡可以连接到提到的机器。连接被拒绝意味着目标主机上没有服务器在侦听套接字。因此,您的客户端代码在这里是无用的。。。你们的异常处理非常糟糕:谢谢,伙计们,但平板电脑是在Wi-fi上的,我观察到的是,当我第一次发送命令时,它可以与平板电脑配合使用,但第二次就不行了。。意味着它会给出例外。请告诉我平板电脑太快了。因此,它在关闭套接字之前再次发送命令,并发生异常。我在每次写之前都在那里添加了sleep1000,现在它可以正常工作了。防火墙导致连接超时,而不是拒绝。拒绝将是对攻击者的信息泄漏。