栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java.net.SocketException: Connection reset卡了我三天,总算解决了

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java.net.SocketException: Connection reset卡了我三天,总算解决了

我写的是客户端,对接远程服务端,报文能成功发送,但是报Connection reset。
这个问题卡了我三天,总算解决了!!!

原因:就是客户端在接受服务端的响应时,没有对循环while ((info = br.readLine()) != null)读出的信息进行判断,导致无法跳出循环直至对端关闭连接,然后客户端抛异常。
必须要判断字符串已经全部取完!!

while ((info = br.readLine()) != null) {
   sb.append(info + "n");
    //这里必须要判断返回值,不然不会跳出循环,直到对端关闭连接抛reset异常
    if(sb.toString().indexOf("")!=-1){
    	break;
    }
}
public class Client {
    
    public void client(String xmlMsgBO, String sn) throws Exception {
        Socket socket = new Socket("xxx.xxx.xxx.xxx", 9052);
        BufferedWriter bw = null;
        BufferedReader br = null;
        try {
            //发送请求
            bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(xmlMsgBO);
            bw.flush();
            socket.shutdownOutput();
            //接收返回信息
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String info = null;
            StringBuffer sb = new StringBuffer();
            while ((info = br.readLine()) != null) {
                sb.append(info + "n");
                //这里必须要判断返回值,不然不会跳出循环,直到对端关闭连接抛reset异常
                if(sb.toString().indexOf("")!=-1){
                	break;
                }
            }
            String str = sb.substring(sb.indexOf("<"), sb.length());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(br);
            IOUtils.closeQuietly(bw);
            IOUtils.closeQuietly(socket);
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/908194.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号