【问题标题】:Android Error : Connection getting reset after reading bufferedreaderAndroid错误:读取bufferedreader后连接被重置
【发布时间】:2019-07-02 04:33:13
【问题描述】:

我在我的 android 应用程序中使用了一种名为 registerDevice() 的方法 用于发送和接收包含多行的特定数据的代码。但我不断收到此错误:

W/System.err: java.net.SocketException: 连接重置

public void registerDevice(){
    final Handler handler = new Handler();
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                Socket s = new Socket(gateway, 1234);
                OutputStream out = s.getOutputStream();
                PrintWriter output = new PrintWriter(out);
                BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                output.println("hello world\r\n");
                output.flush();
                StringBuffer stringBuffer = new StringBuffer("");
                String line = null;
                while ((line = input.readLine()) != null) {
                    stringBuffer.append(line);
                }
                final String st = line.substring(5);
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        if (st.trim().length() != 0)
                            Toast.makeText(getApplicationContext(),st,Toast.LENGTH_LONG).show();
                    }
                });

                output.close();
                out.close();
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();
}

更新:我将代码更改为:

                Socket s = new Socket(gateway, 1234);
                OutputStream out = s.getOutputStream();
                PrintWriter output = new PrintWriter(out);
                BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                output.println("hello world\r\n");
                output.flush();
                final String st = input.readLine();
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        if (st.trim().length() != 0)
                            Toast.makeText(getApplicationContext(),"st",Toast.LENGTH_LONG).show();
                    }
                });

                output.close();
                out.close();
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

我仍然在“final String st = input.readLine();”上遇到异常线。我应该从服务器获取 MAC 地址,然后服务器关闭连接。我检查了服务器,没有任何问题,它在向我发送 MAC 后关闭连接。

【问题讨论】:

  • 你是在哪一行代码得到的?与什么对等代码交互后?
  • @user207421 在这一行“while ((line = input.readLine()) != null)”,当我想第二次调用 input.readline() 时会发生这种情况

标签: java android sockets bufferedreader


【解决方案1】:

伙计们,我发现了问题,是我发送的数据包,我只是将 outout.println() 更改为 output.write(),所以现在服务器识别了我的命令并发送了我需要的数据,这导致了我的输入流不为空并避免异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 2020-07-07
    相关资源
    最近更新 更多