java.net. SocketException: Connection reset

本人遇到的情况并且最终解决:

        //发送
            OutputStream os = socket.getOutputStream();    //字节输出流
            os.write(sendMsg.getBytes(charsetName));
            os.flush();    //刷新缓存,将缓冲输出
            socket.shutdownOutput();
            Thread.sleep(CO_TIMEOUT);    //必须设置等待时间,根据需要设置大小
            AppLogger.debug("发送完成!");
            //接收
            InputStream ips = socket.getInputStream();
            InputStreamReader ipsr = new InputStreamReader(ips);
            BufferedReader br = new BufferedReader(ipsr);
            String s = null;
            while((s = br.readLine()) != null) {
                outStr.append(s);
            }
            socket.shutdownInput();

注:记得关闭连接socket.close();

你可能感兴趣的:(java.net. SocketException: Connection reset)