日期:2014-05-19  浏览次数:20849 次

socket 长连接 心跳包
我们现在需要做一个socket的客户端,与别的公司提供的服务端保持长连接,说明要用心跳包进行长连接,但是用下面的代码好像只能进行第一次正常的连接,之后就会出现被服务端超时强制中断,求高人指点迷津:

Java code

package com.haozhong.recharge.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.SocketChannel;
import java.util.Random;

import com.haozhong.recharge.constants.Constants;

// 取参例子 0030    lotterygetcs    101531    7    lt    lot    

public class ClientTest {
    public static void main(String[] args){
        SocketChannel channel = null ;
        try {
            channel = SocketChannel.open();
            InetAddress add = InetAddress.getByName(Constants.LOTTERY_URL) ;
            InetSocketAddress isa = new InetSocketAddress(add,Constants.LOTTERY_PORT);
            channel.connect(isa) ;
            System.out.println("与服务器建立连接") ;
            /**
             * 
             */
            Socket socket = channel.socket() ;
            while(true){
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os,true);
                String msg = null ;
                char[] cbuf = new char[1024];
                byte[] b = null ;
                StringBuilder sb = new StringBuilder() ;
                String str = "" ;
                msg = "0013\ttest\t"+new Random().nextInt(1000000)+"" ;
                System.out.println(msg) ;
                pw.println(msg);
                while(true){
                    if (br.read(cbuf) == -1){
                        break;
                    }
                    sb.append(new String(cbuf)) ;
                    str = sb.toString() ;
                    b = str.getBytes() ;
                    if(b.length >= 1024){
                        break ;
                    }
                }
                System.out.println(str) ;
                Thread.sleep(5*1000) ;
                /*if(result != null && result != ""){
                    msg ="0032\tlotterygetcs\t33920001\t7\tlj\tlot\t"  ;
                    pw.println(msg);
                    System.out.println(br.readLine()) ;
                    msg = "0100\tlotterygetbulletin\t858939\t60\t33920001\tlot\t2009001\t" ;
                    pw.println(msg) ;
                    System.out.println(br.readLine()) ;
                }*/
            }
            /*while((msg = r.readLine())!= null){
                pw.println(msg);
                System.out.println(br.readLine());
                if(msg.equals("bye")){
                    break ;
                }
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(channel != null){
                try {
                    channel.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}




------解决方案--------------------
请对方给你们一个客户端的“参考实现”,既然他们说别人都是没问题的。

主要怀疑的是所传输内容跟服务器端认可的协议机制不同。

另外,心跳检测这个东西,建议全部修改为异步通讯模型,同步通讯模型不太合适。
append步骤的使用