Graceful Java Programming 优雅Java编程 之Socket Client
老久没有动手写Socket程序了,今天应同事的要求上了一段程序。
这是一段很简单与C++编写的服务端通讯的java客户端,咋一看上去,没有任何问题。
貌似没有问题的程序
Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=%09public%20static%20String%20sendSynMsg(String%20ipAddr%2C%20byte%5B%5D%20datas)%20throws%20Exception%7B%0A%09%09%2F%2F%E8%A7%A3%E6%9E%90%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%9C%B0%E5%9D%80%E5%92%8C%E7%AB%AF%E5%8F%A3%E5%8F%B7%0A%09%09int%20dotPos%20%3D%20ipAddr.indexOf('%3A')%3B%0A%09%09String%20ip%20%3D%20ipAddr.substring(0%2C%20dotPos).trim()%3B%0A%09%09int%20port%20%3D%20Integer.parseInt(ipAddr.substring(dotPos%2B1).trim())%3B%0A%09%09InetSocketAddress%20endpoint%20%3D%20new%20InetSocketAddress(ip%20%2C%20port)%3B%0A%09%09%0A%09%09Socket%20socket%20%3D%20null%3B%0A%09%09OutputStream%20out%20%3D%20null%3B%0A%09%09InputStream%20in%20%3D%20null%3B%0A%09%09try%09%7B%09%09%0A%09%09%09socket%20%3D%20new%20Socket()%3B%0A%09%09%09%2F%2F%E8%AE%BE%E7%BD%AE%E5%8F%91%E9%80%81%E9%80%97%E7%95%99%E6%97%B6%E9%97%B42%E7%A7%92%0A%09%09%09socket.setSoLinger(true%2C%202)%3B%20%0A%09%09%09%2F%2F%E8%AE%BE%E7%BD%AEInputStream%E4%B8%8A%E8%B0%83%E7%94%A8%20read()%E9%98%BB%E5%A1%9E%E8%B6%85%E6%97%B6%E6%97%B6%E9%97%B42%E7%A7%92%0A%09%09%09socket.setSoTimeout(2000)%3B%0A%09%09%09%2F%2F%E8%AE%BE%E7%BD%AEsocket%E5%8F%91%E5%8C%85%E7%BC%93%E5%86%B2%E4%B8%BA32k%EF%BC%9B%0A%09%09%09socket.setSendBufferSize(32*1024)%3B%0A%09%09%09%2F%2F%E8%AE%BE%E7%BD%AEsocket%E5%BA%95%E5%B1%82%E6%8E%A5%E6%94%B6%E7%BC%93%E5%86%B2%E4%B8%BA32k%0A%09%09%09socket.setReceiveBufferSize(32*1024)%3B%0A%09%09%09%2F%2F%E5%85%B3%E9%97%ADNagle%E7%AE%97%E6%B3%95.%E7%AB%8B%E5%8D%B3%E5%8F%91%E5%8C%85%0A%09%09%09socket.setTcpNoDelay(true)%3B%0A%09%09%09%2F%2F%E8%BF%9E%E6%8E%A5%E6%9C%8D%E5%8A%A1%E5%99%A8%0A%09%09%09socket.connect(endpoint)%3B%0A%09%09%09%2F%2F%E8%8E%B7%E5%8F%96%E8%BE%93%E5%87%BA%E8%BE%93%E5%85%A5%E6%B5%81%0A%09%09%09out%20%3D%20socket.getOutputStream()%3B%0A%09%09%09in%20%3D%20socket.getInputStream()%3B%0A%09%09%09%2F%2F%E8%BE%93%E5%87%BA%E8%AF%B7%E6%B1%82%09%09%09%0A%09%09%09out.write(datas)%3B%0A%09%09%09out.flush()%3B%0A%09%09%09%2F%2F%E6%8E%A5%E6%94%B6%E5%BA%94%E7%AD%94%0A%09%09%09BufferedReader%20br%20%3D%20new%20BufferedReader(%20new%20InputStreamReader(in)%20%2C%204096)%3B%0A%09%09%09StringWriter%20received%20%3D%20new%20StringWriter(4096)%3B%0A%09%09%09char%5B%5D%20charBuf%20%3D%20new%20char%5B4096%5D%3B%0A%09%09%09int%20size%20%3D%200%3B%0A%09%09%09while%20((size%20%3D%20br.read(charBuf))%20%3E%200)%7B%0A%09%09%09%09received.write(charBuf%2C%200%2C%20size)%3B%0A%09%09%09%7D%0A%09%09%09return%20received.toString()%3B%0A%09%09%09%0A%09%09%7D%20finally%20%7B%0A%09%09%09if%20(out%20!%3D%20null)%20%7B%0A%09%09%09%09try%20%7B%0A%09%09%09%09%09out.close()%3B%0A%09%09%09%09%7D%20catch(Exception%20ex)%20%7B%0A%09%09%09%09%09ex.printStackTrace()%3B%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09if%20(in%20!%3D%20null)%20%7B%0A%09%09%09%09try%20%7B%0A%09%09%09%09%09in.close()%3B%0A%09%09%09%09%7D%20catch(Exception%20ex)%20%7B%0A%09%09%09%09%09ex.printStackTrace()%3B%0A%09%09%09%09%7D%0A%09%09%09%7D%09%09%0A%09%09%09if%20(socket%20!%3D%20null)%20%7B%0A%09%09%09%09try%20%7B%0A%09%09%09%09%09socket.close()%3B%0A%09%09%09%09%7D%20catch(Exception%20ex)%20%7B%0A%09%09%09%09%09ex.printStackTrace()%3B%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%09%09%09%09%0A%09%7D%0A" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1234166271955" lk_media="yes">
- public static String sendSynMsg(String ipAddr, byte[] datas) throws Exception{
- //解析服务器地址和端口号
- int dotPos = ipAddr.indexOf(':');
- String ip = ipAddr.substring(0, dotPos).trim();
- int port = Integer.parseInt(ipAddr.substring(dotPos+1).trim());
- InetSocketAddress endpoint = new InetSocketAddress(ip , port);
-
- Socket socket = null;
- OutputStream out = null;
- InputStream in = null;
- try {
- socket = new Socket();
- //设置发送逗留时间2秒
- socket.setSoLinger(true, 2);
- //设置InputStream上调用 read()阻塞超时时间2秒
- socket.setSoTimeout(2000);
- //设置socket发包缓冲为32k;
- socket.setSendBufferSize(32*1024);
- //设置socket底层接收缓冲为32k
- socket.setReceiveBufferSize(32*1024);
- //关闭Nagle算法.立即发包
- socket.setTcpNoDelay(true);
- //连接服务器
- socket.connect(endpoint);
- //获取输出输入流
- out = socket.getOutputStream();
- in = socket.getInputStream();
- //输出请求
- out.write(datas);
- out.flush();
- //接收应答
- BufferedReader br = new BufferedReader( new InputStreamReader(in) , 4096);
- StringWriter received = new StringWriter(4096);
- char[] charBuf = new char[4096];
- int size = 0;
- while ((size = br.read(charBuf)) > 0){
- received.write(charBuf, 0, size);
- }
- return received.toString();
-
- } finally {
- if (out != null) {
- try {
- out.close();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- if (in != null) {
- try {
- in.close();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- if (socket != null) {
- try {
- socket.close();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- }
public static String sendSynMsg(String ipAddr, byte[] datas) throws Exception{ //解析服务器地址和端口号 int dotPos = ipAddr.indexOf(':'); String ip = ipAddr.substring(0, dotPos).trim(); int port = Integer.parseInt(ipAddr.substring(dotPos+1).trim()); InetSocketAddress endpoint = new InetSocketAddress(ip , port); Socket socket = null; OutputStream out = null; InputStream in = null; try { socket = new Socket(); //设置发送逗留时间2秒 socket.setSoLinger(true, 2); //设置InputStream上调用 read()阻塞超时时间2秒 socket.setSoTimeout(2000); //设置socket发包缓冲为32k; socket.setSendBufferSize(32*1024); //设置socket底层接收缓冲为32k socket.setReceiveBufferSize(32*1024); //关闭Nagle算法.立即发包 socket.setTcpNoDelay(true); //连接服务器 socket.connect(endpoint); //获取输出输入流 out = socket.getOutputStream(); in = socket.getInputStream(); //输出请求 out.write(datas); out.flush(); //接收应答 BufferedReader br = new BufferedReader( new InputStreamReader(in) , 4096); StringWriter received = new StringWriter(4096); char[] charBuf = new char[4096]; int size = 0; while ((size = br.read(charBuf)) > 0){ received.write(charBuf, 0, size); } return received.toString(); } finally { if (out != null) { try { out.close(); } catch(Exception ex) { ex.printStackTrace(); } } if (in != null) { try { in.close(); } catch(Exception ex) { ex.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch(Exception ex) { ex.printStackTrace(); } } } }
但实际的调试中,总是报Read TimeOut异常!!排查原因后发现,数据是接收到了,只是size = br.read(charBuf)不会返回-1(java doc中的说明是读取到结束时size会返回-1)。
对于编写服务器端程序的C++程序员而言,他们通常会在通讯结束的时候,在数据的尾部加上一个\0的结束字符。因此,我们针对此做了修正
出问题的程序段
Java代码
- while ((size = br.read(charBuf)) > 0){
- received.write(charBuf, 0, size);
- }
while ((size = br.read(charBuf)) > 0){ received.write(charBuf, 0, size); }
修改后的正确写法
Java代码
- char lastChar = 0;
- do {
- size = br.read(charBuf , 0 , 4096);
- lastChar = charBuf[size-1];
- if(lastChar == 0){
- //去除尾部的\0字符
- received.write(charBuf, 0, size - 1);
- }
- }while(lastChar != 0);
char lastChar = 0; do { size = br.read(charBuf , 0 , 4096); lastChar = charBuf[size-1]; if(lastChar == 0){ //去除尾部的\0字符 received.write(charBuf, 0, size - 1); } }while(lastChar != 0);