java tcp socket readline 阻塞问题处理(Handling of Java TCP socket readLine blocking)

背景:项目需要对接物联网设备,要求用TCP SOCKET 设备的服务端,设备自动上报自身状态及指标数据等信息。

问题:提交参数后,读取服务端的BufferedReader,一直卡在read line那里,一直等到一分多钟才读取到。

分析:既然能读取到,说明提交参数和连接等是没有问题的,问题就出在读取这里。还是在网上搜索解决方法,更改为字节读取,不使用行读取,问题解决。大概是服务端写根本就没有一行一行的写。

代码如下:

 @Test
    void test5() throws IOException {
        InetSocketAddress inetSocketAddress = new InetSocketAddress
                ("", 10965);
        Socket socket = new Socket();
        System.out.println("开始连接socket服务器");
        socket.connect(inetSocketAddress);
        if (socket.isConnected()) {
            System.out.println("服务器连接成功");
        } else {
            System.out.println("服务器连接失败");
            return;
        }
        System.out.println("开始写入数据");
        OutputStream os = socket.getOutputStream();//字节输出流
        PrintWriter pw = new PrintWriter(os);//将输出流包装成打印流
        String parm = "{\n" +
                "    \"CMD\":\"CSREG\",\n" +
                "    \"Token\":4,\n" +
                "    \"CsEUI\":\"626F6373bb46da52\",\n" +
                "    \"AppNonce\":292657,\n" +
                "    \"Challenge\":\"ABCDEF1234567890ABCDEF1234292657\"\n" +
                "}\0";
        System.out.println("数据:" + parm);
        pw.write(parm);
        pw.flush();
        System.out.println("数据写入完成,开始读取数据");
        InputStream is = socket.getInputStream();
        byte[] b = new byte[1024];
        int x = is.read(b, 0, b.length);
        String string = new String(b, 0, x);
//        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
//        String info = br.readLine();
        System.out.println("服务器:" + string);
        System.out.println("执行完成");
//        br.close();
        is.close();
        pw.close();
        os.close();
        socket.close();
    }
————————

Background: the project needs to connect Internet of things equipment. It is required to use the server of TCP socket equipment, and the equipment automatically reports its own status, index data and other information.

Problem: after submitting the parameters, read the BufferedReader on the server side, stuck in the read line, and didn’t read it until more than one minute.

Analysis: since it can be read, it indicates that there is no problem with submitting parameters and connections. The problem lies in reading. Search for solutions on the Internet and change it to byte reading instead of row reading. The problem is solved. Maybe the server doesn’t write line by line at all.

The code is as follows:

 @Test
    void test5() throws IOException {
        InetSocketAddress inetSocketAddress = new InetSocketAddress
                ("", 10965);
        Socket socket = new Socket();
        System.out.println("开始连接socket服务器");
        socket.connect(inetSocketAddress);
        if (socket.isConnected()) {
            System.out.println("服务器连接成功");
        } else {
            System.out.println("服务器连接失败");
            return;
        }
        System.out.println("开始写入数据");
        OutputStream os = socket.getOutputStream();//字节输出流
        PrintWriter pw = new PrintWriter(os);//将输出流包装成打印流
        String parm = "{\n" +
                "    \"CMD\":\"CSREG\",\n" +
                "    \"Token\":4,\n" +
                "    \"CsEUI\":\"626F6373bb46da52\",\n" +
                "    \"AppNonce\":292657,\n" +
                "    \"Challenge\":\"ABCDEF1234567890ABCDEF1234292657\"\n" +
                "}\0";
        System.out.println("数据:" + parm);
        pw.write(parm);
        pw.flush();
        System.out.println("数据写入完成,开始读取数据");
        InputStream is = socket.getInputStream();
        byte[] b = new byte[1024];
        int x = is.read(b, 0, b.length);
        String string = new String(b, 0, x);
//        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
//        String info = br.readLine();
        System.out.println("服务器:" + string);
        System.out.println("执行完成");
//        br.close();
        is.close();
        pw.close();
        os.close();
        socket.close();
    }