我用ComMax模拟客户端发起请求,业务处理流程正常,但是没有返回报文:
TX:111111201109141406231111111111111111111115000287452        5000  (09:44:12:077)
和其他模块联调时业务处理流程正常,可是那边客户端接收不到我回写的SOCKET报文,报连接超时错误,请高手帮忙看看:
public class SocketServer {
public SocketServer(){
        Socket incoming;
        ServerSocket so;
        try{
            so= new ServerSocket(9731);
            System.out.println("等待客户端连接");
         
            while(true){
                try{
                      incoming = so.accept( );
                      System.out.println("已连接客户端");
                      GetInfo gi=new GetInfo(incoming);
                 } catch (IOException e){
                 so.close();
                    e.printStackTrace();
                 }
            }
        }catch (IOException e){
             e.printStackTrace();
        }
    }    public static void main(String[] args){ 
      PropertyConfigurator.configure("E:\\Myeclipse\\workspace\\oufeitest\\src\\oufei\\test\\log4j.properties");
      new SocketServer();
    }
    private static class GetInfo implements Runnable{   //�߳���
        private Socket incoming;
        private String s=null;
        private BufferedReader b;
        private BufferedWriter c;
        Thread t;        public GetInfo(Socket incoming){
            try{
                this.incoming=incoming;
                b=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
//                c=new BufferedWriter(new OutputStreamWriter(incoming.getOutputStream())); 
                t=new Thread(this);
                t.start();
            }catch(IOException e){
                e.printStackTrace();
            }    
        }
          
        public void run(){
            try{
               Logger logger=Logger.getLogger(SocketServer.class);
             String lines;
             String line="";
                while ((lines = b.readLine()) != null){
                 line+=lines;
                }
                System.out.println("line----"+line);
                System.out.println(line.length());
                if(line.length()%63!=0){
                 logger.info("交换平台发送的报文长度不正确!");
                 c.write("");
                
                } else {
                 List<String> list1 = new ArrayList<String>();
                    for(int i=0;i<line.length();i+=63){
                     list1.add(line.substring(i,i+63));
                    }
                    Iterator list=list1.iterator();
                 while(list.hasNext()){
                     String co="";
         co=(String) list.next();            
                         logger.info("后台socket发送给我的内容:"+co);
                 String post_url="http://esales1.ofcard.com:8088/onlineorder.do";
                            String content=ConcatPackage.getPackage(co);
                            //用post方式发送http请求
                 String xmlcontent="";
                 try {
                  xmlcontent=Sender.readContentFromPost(post_url, content);
                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 System.out.println("不好啦,发送http请求出问题啦,哦也!");
                 e.printStackTrace();
                 }
                 if(xmlcontent.equals("04")){
                 logger.info("号段错误");
                     c.write("111111                                                ");
                 } else if(xmlcontent.equals("14")) {
                 logger.info("金额错误");
                 c.write("111111                                                ");
                 } else {
                        System.out.println("查看取得的xml字符串是否为空:"+xmlcontent);
                     System.out.println("发送欧非任务已完成");
                     //处理接收xml,并以socket发送到交换平台
                     Decodexml decoderxml = new Decodexml();
                     String packagesend = decoderxml.xmlElements(xmlcontent);  //xml字符串被解析并被拼装为package
                         try {
                          System.out.println("组装的发送后台的SOCKET为:"+"["+packagesend+"]");
                          logger.info("c.write前"+packagesend);
                                    c=new BufferedWriter(new OutputStreamWriter(incoming.getOutputStream())); 
                          c.write(packagesend);
                          c.toString();
                          System.out.println("c.toString"+c.toString());
//                          c.newLine();
//                          c.flush();
//                          c.close();
                          logger.info("c.write后----------------");
//                     Communicater.sendRequest(packagesend);
                     } catch (IOException e) {
                     System.out.println("发送给后台的socket失败!");
                     e.printStackTrace();
                     }        
                 }
 
                       }
                
             }
            
            }catch(IOException e){
                e.printStackTrace();
            }        }
    }
   
 }

解决方案 »

  1.   

    System.out.println("不好啦,发送http请求出问题啦,哦也!");楼主写代码时很欢乐   
    慢慢调试嘛 看哪里出问题了
      

  2.   

    你先得把代码简化掉,只留下socket的收发,其它的逻辑去掉。
    你现在这么一堆代码,命名也极不规范,确实看的头晕。
      

  3.   

    public TestSocketServer(){
        Socket incoming;
        ServerSocket so;
        BufferedReader b;
        BufferedWriter c;
        OutputStreamWriter d;    try{
            so= new ServerSocket(9731);
            System.out.println("等待客户端连接。");
         
            while(true){
                try{
                    incoming = so.accept( );
                      System.out.println("已连接客户端。");
                      b=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
                      String lines;
                     String line="";
                      while ((lines = b.readLine()) != null){
                       line+=lines;
                      }
                   List<String> list1 = new ArrayList<String>();
                    for(int i=0;i<line.length();i+=63){
                     list1.add(line.substring(i,i+63));
                    }
                    Iterator list=list1.iterator();
                    while(list.hasNext()){
                      String co="";
        co=(String) list.next();            
        
              String post_url="http://esales1.ofcard.com:8088/onlineorder.do";
                         String content=ConcatPackage.getPackage(co);
                         //用post方式发送http请求
              String xmlcontent="";
              try {
               xmlcontent=Sender.readContentFromPost(post_url, content);
              } catch (IOException e) {
              // TODO Auto-generated catch block
              System.out.println("不好啦,发送http请求出问题啦,哦也!");
              e.printStackTrace();
              }
             Decodexml decoderxml = new Decodexml();
             String packagesend = decoderxml.xmlElements(xmlcontent);  //xml字符串被解析并被拼装为package
                 try {
                  System.out.println("组装的发送后台的SOCKET为:"+"["+packagesend+"]");
                            c=new BufferedWriter(new OutputStreamWriter(incoming.getOutputStream()));
                  c.write(packagesend);
                  c.toString();
                  System.out.println("c.toString"+c.toString());
             } catch (IOException e) {
             System.out.println("发送给后台的socket失败!");
             e.printStackTrace();
             }
                    
                    }
                 } catch (IOException e){
                 so.close();
                    e.printStackTrace();
                 }
            }
        }catch (IOException e){
             e.printStackTrace();
        }
    }public static void main(String[] args){ 
         PropertyConfigurator.configure("E:\\Myeclipse\\workspace\\oufeitest\\src\\oufei\\test\\log4j.properties");
         new SocketServer();
    }
    }这是简化后的代码,还是不能接收到返回给客户端的报文
      

  4.   

    flush试了没用,但是当我改了下 b=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
      String lines;
      String line="";
      while ((lines = b.readLine()) != null){
      line+=lines;
      }
    改为 public String read(InputStream is) throws IOException {
    String returnBuff = null;
    try {
    byte[] buf = new byte[BUFF_LEN]; int len = is.read(buf);
    returnBuff = new String(buf, 0, len);
    } catch (IOException e) {
    e.printStackTrace();
    System.exit(-1);
    } finally {
    // is.close();
    }
    return returnBuff;
    }
    当我去调用这个方法时就可以有返回了,但是其中的原因我不太明白,希望高手能讲解下,谢谢
      

  5.   

    调用代码如下:             line=rw.read(incoming.getInputStream());