Java Socket實戰之四 傳輸壓縮對象

本文地址:http://blog.csdn.net/kongxx/article/details/7259834

Java Socket實戰之一 單線程通信

Java Socket實戰之二 多線程通信

Java Socket實戰之三 傳輸對象

上一篇文章說到了用Java Socket來傳輸對象,但是在有些情況下比如網絡環境不好或者對象比較大的情況下需要把數據對象進行壓縮然後在傳輸,此時就需要壓縮這些對象流,此時就可以GZIPInputStream和GZIPOutputStream來處理一下socket的InputStream和OutputStream。

仍然需要一個實現了java.io.Serializable接口的簡單Java對象

  1. package com.googlecode.garbagecan.test.socket.sample4;  
  2.   
  3. public class User implements java.io.Serializable {  
  4.     private static final long serialVersionUID = 1L;  
  5.     private String name;  
  6.     private String password;  
  7.   
  8.     public User() {  
  9.           
  10.     }  
  11.       
  12.     public User(String name, String password) {  
  13.         this.name = name;  
  14.         this.password = password;  
  15.     }  
  16.       
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.   
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.   
  25.     public String getPassword() {  
  26.         return password;  
  27.     }  
  28.   
  29.     public void setPassword(String password) {  
  30.         this.password = password;  
  31.     }  
  32.       
  33. }  
在Server端使用,socket的InputStream首先被包裝成GZIPInputStream,然後又被包裝成ObjectInputStream,而socket的OutputStream首先被包裝成GZIPOutputStream,然後又被包裝成ObjectOutputStream,如下:

  1. package com.googlecode.garbagecan.test.socket.sample4;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.ObjectInputStream;  
  5. import java.io.ObjectOutputStream;  
  6. import java.net.ServerSocket;  
  7. import java.net.Socket;  
  8. import java.util.logging.Level;  
  9. import java.util.logging.Logger;  
  10. import java.util.zip.GZIPInputStream;  
  11. import java.util.zip.GZIPOutputStream;  
  12.   
  13. public class MyServer {  
  14.   
  15.     private final static Logger logger = Logger.getLogger(MyServer.class.getName());  
  16.       
  17.     public static void main(String[] args) throws IOException {  
  18.         ServerSocket server = new ServerSocket(10000);  
  19.   
  20.         while (true) {  
  21.             Socket socket = server.accept();  
  22.             socket.setSoTimeout(10 * 1000);  
  23.             invoke(socket);  
  24.         }  
  25.     }  
  26.   
  27.     private static void invoke(final Socket socket) throws IOException {  
  28.         new Thread(new Runnable() {  
  29.             public void run() {  
  30.                 GZIPInputStream gzipis = null;  
  31.                 ObjectInputStream ois = null;  
  32.                 GZIPOutputStream gzipos = null;  
  33.                 ObjectOutputStream oos = null;  
  34.                   
  35.                 try {  
  36.                     gzipis = new GZIPInputStream(socket.getInputStream());  
  37.                     ois = new ObjectInputStream(gzipis);  
  38.                     gzipos = new GZIPOutputStream(socket.getOutputStream());  
  39.                     oos = new ObjectOutputStream(gzipos);  
  40.   
  41.                     Object obj = ois.readObject();  
  42.                     User user = (User)obj;  
  43.                     System.out.println("user: " + user.getName() + "/" + user.getPassword());  
  44.   
  45.                     user.setName(user.getName() + "_new");  
  46.                     user.setPassword(user.getPassword() + "_new");  
  47.   
  48.                     oos.writeObject(user);  
  49.                     oos.flush();  
  50.                     gzipos.finish();  
  51.                 } catch (IOException ex) {  
  52.                     logger.log(Level.SEVERE, null, ex);  
  53.                 } catch(ClassNotFoundException ex) {  
  54.                     logger.log(Level.SEVERE, null, ex);  
  55.                 } finally {  
  56.                     try {  
  57.                         ois.close();  
  58.                     } catch(Exception ex) {}  
  59.                     try {  
  60.                         oos.close();  
  61.                     } catch(Exception ex) {}  
  62.                     try {  
  63.                         socket.close();  
  64.                     } catch(Exception ex) {}  
  65.                 }  
  66.             }  
  67.         }).start();  
  68.     }  
  69. }  
Client也和Server端類似,同樣要不socket的XXXStream包裝成GZIPXXXStream,然後再包裝成ObjectXXXStream,如下:

  1. package com.googlecode.garbagecan.test.socket.sample4;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.ObjectInputStream;  
  5. import java.io.ObjectOutputStream;  
  6. import java.net.InetSocketAddress;  
  7. import java.net.Socket;  
  8. import java.net.SocketAddress;  
  9. import java.util.logging.Level;  
  10. import java.util.logging.Logger;  
  11. import java.util.zip.GZIPInputStream;  
  12. import java.util.zip.GZIPOutputStream;  
  13.   
  14. public class MyClient {  
  15.       
  16.     private final static Logger logger = Logger.getLogger(MyClient.class.getName());  
  17.       
  18.     public static void main(String[] args) throws Exception {  
  19.         for (int i = 0; i < 10; i++) {  
  20.             Socket socket = null;  
  21.             GZIPOutputStream gzipos = null;  
  22.             ObjectOutputStream oos = null;  
  23.             GZIPInputStream gzipis = null;  
  24.             ObjectInputStream ois = null;  
  25.               
  26.             try {  
  27.                 socket = new Socket();  
  28.                 SocketAddress socketAddress = new InetSocketAddress("localhost"10000);   
  29.                 socket.connect(socketAddress, 10 * 1000);  
  30.                 socket.setSoTimeout(10 * 1000);  
  31.                   
  32.                 gzipos = new GZIPOutputStream(socket.getOutputStream());  
  33.                 oos = new ObjectOutputStream(gzipos);  
  34.                 User user = new User("user_" + i, "password_" + i);  
  35.                 oos.writeObject(user);  
  36.                 oos.flush();  
  37.                 gzipos.finish();  
  38.                   
  39.                 gzipis = new GZIPInputStream(socket.getInputStream());  
  40.                 ois = new ObjectInputStream(gzipis);  
  41.                 Object obj = ois.readObject();  
  42.                 if (obj != null) {  
  43.                     user = (User)obj;  
  44.                     System.out.println("user: " + user.getName() + "/" + user.getPassword());  
  45.                 }  
  46.             } catch(IOException ex) {  
  47.                 logger.log(Level.SEVERE, null, ex);  
  48.             } finally {  
  49.                 try {  
  50.                     ois.close();  
  51.                 } catch(Exception ex) {}  
  52.                 try {  
  53.                     oos.close();  
  54.                 } catch(Exception ex) {}  
  55.                 try {  
  56.                     socket.close();  
  57.                 } catch(Exception ex) {}  
  58.             }  
  59.         }  
  60.     }  
  61. }  


最後測試上面的代碼,首先運行Server類,然後運行Client類,就可以分別在Server端和Client端控制檯看到接收到的User對象實例了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章