首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重新尝试丢失的连接,以获得可靠的传输?

如何重新尝试丢失的连接,以获得可靠的传输?
EN

Stack Overflow用户
提问于 2014-05-19 06:42:52
回答 1查看 98关注 0票数 0

我正在通过套接字编程将xml数据发送到服务器。我发现,有时当服务器停机,客户端报告套接字超时时,我无法发送或接收。

我想处理这个异常,并尝试重新发送3到4次。我应该使用Thread.sleep,写循环,还是有更好的方法?

代码语言:javascript
复制
   private String sendRequestToChannel(String request) {

    String xmlData = null;
    BufferedReader rd = null;
    BufferedWriter bw = null;
    String line = null;
    String lineSep = null;
    String data = null;
    StringBuffer serverData = null;


    try {

        Socket cliSocket = new Socket();

        cliSocket.connect(new InetSocketAddress(HOST, PORT), SOCKET_TIMEOUT);

        bw = new BufferedWriter(new OutputStreamWriter(cliSocket.getOutputStream()));

        bw.write("POST " + PATH + " HTTP/1.0\r\n");
        bw.write("Host: " + HOST + "\r\n");
        bw.write("Content-Length: " + request.length() + "\r\n");
        bw.write("Pragma: cache\r\n");
        bw.write("Cache-Control: private, must-revalidate\r\n");
        bw.write("Content-Type: application/x-www-form-urlencoded\r\n");
        bw.write("\r\n");
        bw.write(request);
        bw.flush();

        rd = new BufferedReader(new InputStreamReader(cliSocket.getInputStream()));

        System.out.println("Step 4 : Getting Input Stream");
        serverData = new StringBuffer("");


        lineSep = System.getProperty("line.separator");
        while ((line = rd.readLine()) != null) {
            serverData.append(line);
            serverData.append(lineSep);
        }

        data = serverData.toString();
        int index = data.indexOf("<");

        if (index != -1) {
            xmlData = data.substring(index);

        } else {
            System.out.println("\r\n \r\n  XML Data Not Retrived");
        }


    } catch (java.net.UnknownHostException uh) {
        uh.printStackTrace();
        System.out.println("$$$$$$$$$$$$  in sendRequestToChannel : UnknownHostException " + uh.getMessage());
        return "   in sendRequestToChannel : UnknownHostException " + uh.toString();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.out.println("$$$$$$$$$$$$  in sendRequestToChannel :  IOException " + ioe.getMessage());
        return " in sendRequestToChannel :   IOException " + ioe.toString();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("$$$$$$$$$$$$  in sendRequestToChannel :  Exception " + e.getMessage());
        return " in sendRequestToChannel :  Exception " + e.toString();
    } finally {
        try {
            if (bw != null) {
                bw.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(SA_Caesar.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            if (rd != null) {
                rd.close();

            }
        } catch (IOException ex) {
            Logger.getLogger(SA_Caesar.class.getName()).log(Level.SEVERE, null, ex);



        }

        bw = null;
        rd = null;
        line = null;
        lineSep = null;
        data = null;
        serverData = null;

    }
    return xmlData;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-19 07:11:51

应用程序在其远程服务崩溃时面临的最大问题是,根本无法预测何时返回(如果有的话)。

您已经在使用TCP连接,当面临短期无法访问的服务时,该连接将重新尝试,但是当TCP声明连接失效时,您实际上不会通过自动重新建立连接来提高可靠性。例如,如果远程服务器返回需要两天的时间,您的应用程序是否能够像从未中断过一样工作呢?所有必要的数据会在中断期间排队吗?在周末的失败过程中,连接系统的语义会保持不变吗?如果远程服务中断5天,情况如何?

从系统的角度来看,您能做的最好的事情就是通知操作符有一个需要注意的错误。正因如此,我们仍有营办商,并会在可预见的将来继续经营。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23731226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档