Graceful Java Programming 优雅Java编程 之Socket Client 2

最终的完整程序段:
Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" >
  1. private static String sendSynMsg(String ipAddr, byte[] datas) throws Exception{  
  2.     //解析服务器地址和端口号  
  3.     int dotPos = ipAddr.indexOf(':');  
  4.      String ip = ipAddr.substring(0, dotPos).trim();  
  5.     int port = Integer.parseInt(ipAddr.substring(dotPos+1).trim());  
  6.      InetSocketAddress endpoint = new InetSocketAddress(ip , port);  
  7.       
  8.      Socket socket = null;  
  9.      OutputStream out = null;  
  10.      InputStream in = null;  
  11.     try {         
  12.          socket = new Socket();  
  13.         //设置发送逗留时间2秒  
  14.          socket.setSoLinger(true, 2);   
  15.         //设置InputStream上调用 read()阻塞超时时间2秒  
  16.          socket.setSoTimeout(2000);  
  17.         //设置socket发包缓冲为32k;  
  18.          socket.setSendBufferSize(32*1024);  
  19.         //设置socket底层接收缓冲为32k  
  20.          socket.setReceiveBufferSize(32*1024);  
  21.         //关闭Nagle算法.立即发包  
  22.          socket.setTcpNoDelay(true);  
  23.         //连接服务器  
  24.          socket.connect(endpoint);  
  25.         //获取输出输入流  
  26.          out = socket.getOutputStream();  
  27.          in = socket.getInputStream();  
  28.         //输出请求            
  29.          out.write(datas);  
  30.          out.flush();  
  31.         //接收应答  
  32.          BufferedReader br = new BufferedReader( new InputStreamReader(in) , 4096);  
  33.          StringWriter received = new StringWriter(4096);  
  34.         char[] charBuf = new char[4096];  
  35.         int size = 0;  
  36.         char lastChar = 0;  
  37.         do {  
  38.              size = br.read(charBuf , 0 , 4096);  
  39.              lastChar = charBuf[size-1];  
  40.             if(lastChar == 0){  
  41.                  received.write(charBuf, 0, size - 1);  
  42.              }  
  43.             //System.out.println(received.toString());  
  44.          }while(lastChar != 0);  
  45.           
  46.         return received.toString();  
  47.           
  48.      } finally {  
  49.         if (out != null) {  
  50.             try {  
  51.                  out.close();  
  52.              } catch(Exception ex) {  
  53.                  ex.printStackTrace();  
  54.              }  
  55.          }  
  56.         if (in != null) {  
  57.             try {  
  58.                  in.close();  
  59.              } catch(Exception ex) {  
  60.                  ex.printStackTrace();  
  61.              }  
  62.          }         
  63.         if (socket != null) {  
  64.             try {  
  65.                  socket.close();  
  66.              } catch(Exception ex) {  
  67.                  ex.printStackTrace();  
  68.              }  
  69.          }  
  70.      }                 
  71. }  
private 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; char lastChar = 0; do { size = br.read(charBuf , 0 , 4096); lastChar = charBuf[size-1]; if(lastChar == 0){ received.write(charBuf, 0, size - 1); } //System.out.println(received.toString()); }while(lastChar != 0); 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(); } } } }

程序中的关键点:
1. 不直接使用new Socket(String ip , int port)的构造函数,而是设置了socket的环境参数后再连接
2.设置发送逗留时间 socket.setSoLinger(true, 2); 这个参数是socket发送数据时的超时,如果对方在固定时间内不接受,则关闭socket。与socket.setSoTimeout(2000)不 同,这个是设置InputStream上调用 read()阻塞超时时间。
3.socket.setTcpNoDelay(true);关闭Nagle算法。这使得在调用out.flush();时总能第一时间的发送数据包(这个适用于你的数据包是完整的一次性发送的前提)。
4.根据应用协议的实际大小,优化你的接收和发送缓冲,这两个参数可以有效提高网络通信的效率。
5.使用char的数组配合StringWriter作为接收数据的写入,这个比使用readLine方法实现更优雅。

一点小经验,和大家分享

你可能感兴趣的:(programming)