JAVA客户端程序和服务器端Socket交互时 短连接问题
阿军 2013-06-30 12:26:40 大家好,想向各位高手请教一下,我现在开发一个JAVA客户端程序,客户端(client)需与服务器端(server)通过Socket通信,其中服务器端是调用别人的,他们只提供了ip地址、端口号、用户名和密码,我现在处理具体交互流程如下:
1、客户端与服务器建立连接
2、客户端向服务器发送报文信息(用户名和密码登录授权)
3、服务器响应登录成功信息
4、客户端将请求参数做奇偶校验发送到服务器
5、从服务器获取响应数据
我现在遇到的问题是,客户端和服务器端有时可以成功交互,有时却交互不成功。当交互不成功时打印日志发现交互流程停留在第5步,无法从服务器端读取数据。
我也尝试过当出现以上问题时,我关闭socket连接,用Thread线程等待几秒钟后再去执行上面1-5个步骤,但还是停留在第5步。我的详细代码如下:
/************************************************************************************/
// boolean flag = true;//循环标志
// Socket m_socket = null;
// InputStream in=null;
// OutputStream out=null;
// InputStream in1=null;
// int _tryTimes = 1==0?-1:1;
//
// while (flag&&_tryTimes!=0) {
// try {
// m_socket=new Socket();
// m_socket.setSoTimeout(20*1000);
// m_socket.connect(new InetSocketAddress(ip,port),20*1000);
// m_socket.setKeepAlive(true);
// m_socket.setOOBInline(true);
//
// out = m_socket.getOutputStream();
// in = m_socket.getInputStream();
// String msg = "GET /hz12 HTTP/1.1 \r\n";// 请求行
// msg += "Host: rtcm-ntrip.org \r\n";// 请求首部
// msg += "Ntrip-Version: Ntrip/2.0 \r\n";// 通用首部
// msg += "User-Agent: NTRIP GNSSInternetRadio 2.0.10 \r\n";// 请求首部
//// msg += "Accept: */*\r\nConnection: close\r\n";// 7802_RTD
// msg+="Accept:*/* \r\n";//请求首部
// msg += "Authorization: Basic Z3pnc3RhMjAxMzpnemdzdGEyMDEz\r\n\r\n";// base64加密用户名和密码 // 请求首部
// /******************************************************************/
//
// System.out.print("" + msg);
// out.write(msg.getBytes());
// out.flush();
// int n = 0;
// byte[] buf = new byte[1024];
// byte[] buf1 = new byte[1024];
// String message = "";
// String message1 = "";
// if ((n = in.read(buf)) > 0) {
// message += new String(buf, 0, n);
// System.out.println("receive:" + message);
//// System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
// in1 = m_socket.getInputStream();
// System.out.println("经纬度格式转换!!!!!");
// String latString = Arith.formatLonLat(lat);
// String logString = Arith.formatLonLat(log);
// System.out.println("奇偶校验!!!!!");
// String ss = "$GPGGA," + gpsTime + "," + latString + ",N,"+ logString + ",E,1,09,1.0,0.00,M,0.0,M,0,*";
// ss = Gpgga.getGPGGA(ss) + "\r\n\r\n";
//// System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
// System.out.println("GPGGA:" + ss);
//// System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
// out.write(ss.getBytes());
// out.flush();
// _tryTimes--;
//// System.out.println("*******************************************");
// System.out.println("开始解析cors站数据!!!!");
// int m = 0;
// RtcmT rt = new RtcmT();
// rt.message = new int[60];
// while ((m = in1.read(buf1)) > 0) {
// message1 += new String(buf1, 0, m);
//// System.out.println("Rtcm--getRtcmData(); message1:"+ message1);
// char[] buf2 = message1.toCharArray();
// for (int i = 0; i < buf2.length; i++) {
// rt.new_byte(buf2[i]);
// }
// message1 = "";
// if (rt.getSatelliteList() != null&& !rt.getSatelliteList().isEmpty()) {
// break;
// }
// }
// System.out.println("cors站数据解析完毕!!!!");
//// System.out.println("Rtcm--getRtcmData();" + "over");
//
// out.close();
// in.close();
// in1.close();
// m_socket.close();
// flag = false;// 停止循环标志
// return rt.getSatelliteList();
// } else {
// if ("" == message && message.equals(""))
// System.out.println("cors站登录失败!!!!!");
// return null;
// }
//
// } catch (SocketException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
//
// if(m_socket!=null){
// try {
// in.close();
// out.close();
// in1.close();
// m_socket.close();
//
// } catch (IOException e2) {
// // TODO Auto-generated catch block
// e2.printStackTrace();
// }
//
// m_socket = null;
// in=null;
// out=null;
// in1=null;
// }
//
//// try {
//// Thread.sleep(5 * 1000);
//// } catch (InterruptedException e1) {
//// // TODO Auto-generated catch block
//// e1.printStackTrace();
//// }
//
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//// finally{
//// if(m_socket!=null){
//// try {
//// m_socket.close();
//// } catch (IOException e) {
//// // TODO Auto-generated catch block
//// e.printStackTrace();
//// }
//// m_socket=null;
//// }
//// in=null;
//// out=null;
//// in1=null;
//// }
//
// }
我也做过多次尝试,但一直没找出解决办法。从网上看到一些资料说TCP通信时区分“长连接”、“短连接”,我调用服务器端可能是“短连接”导致在第2次取(第5步时)数据时已经关闭(服务器断开连接),如果是这种情况,我应该在现有代码中怎样修改?或者改用HTTP通信是否也可以实现?
请各位帮忙出谋划策,指点迷津,尽量详细一点,有Demo程序代码就更好了。再次谢谢大家!!!!