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程序代码就更好了。再次谢谢大家!!!!


...全文
484 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangmei342548343 2014-12-29
  • 打赏
  • 举报
回复
你好 最近我也在做这个但是还没什么好的思路 请问能不能发一个demo给我参考一下 谢谢我邮箱yangmei342548343@163.com
火影之贺 2013-07-01
  • 打赏
  • 举报
回复
为什么不换个思路:服务器在第5步有没有给你响应? 处理这种报文交互最好的方法,就是装个wireshark抓包分析,比看代码有用多了!
阿军 2013-07-01
  • 打赏
  • 举报
回复
有人帮我解答一下吗?
阿军 2013-07-01
  • 打赏
  • 举报
回复
有什么好的思路,请指教一下?
阿军 2013-06-30
  • 打赏
  • 举报
回复
不好意思我是初次发帖,能为我解答一下以上问题吗?谢谢
partys 2013-06-30
  • 打赏
  • 举报
回复
代码太乱了 能不能贴出java格式的?

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧