简体   繁体   English

Android错误:读取bufferedreader后连接重置

[英]Android Error : Connection getting reset after reading bufferedreader

I'm using a method called registerDevice() in my android app code to send and receive specific data which contains multiple lines.我在我的 android 应用程序代码中使用一种称为registerDevice()的方法来发送和接收包含多行的特定数据。 But I keep getting this error:但我不断收到此错误:

W/System.err: java.net.SocketException: Connection reset 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();
}

UPDATE: I changed the code to this:更新:我将代码更改为:

                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();
            }
        }

I'm Still getting exceptions on the "final String st = input.readLine();"我仍然在“final String st = input.readLine();”上遇到异常line.线。 I am supposed to get a MAC address from the server and then the server closes the connection.I checked the server and there is nothing wrong with it, it closes the connection AFTER it sends me the MAC.我应该从服务器获取 MAC 地址,然后服务器关闭连接。我检查了服务器,没有任何问题,它在向我发送 MAC 后关闭连接。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM