Java Socket实战之四:传输压缩对象

Java Socket实战之四:传输压缩对象

上一篇文章说到了用Java Socket来传输对象,但是在有些情况下比如网络环境不好或者对象比较大的情况下需要把数据对象进行压缩然后在传输,此时就需要压缩这些对象流,此时就可以GZIPInputStream和GZIPOutputStream来处理一下socket的InputStream和OutputStream。

仍然需要一个实现了java.io.Serializable接口的简单Java对象:


  1. packagecom.googlecode.garbagecan.test.socket.sample4;
  2. publicclassUserimplementsjava.io.Serializable{
  3. privatestaticfinallongserialVersionUID=1L;
  4. privateStringname;
  5. privateStringpassword;
  6. publicUser(){
  7. }
  8. publicUser(Stringname,Stringpassword){
  9. this.name=name;
  10. this.password=password;
  11. }
  12. publicStringgetName(){
  13. returnname;
  14. }
  15. publicvoidsetName(Stringname){
  16. this.name=name;
  17. }
  18. publicStringgetPassword(){
  19. returnpassword;
  20. }
  21. publicvoidsetPassword(Stringpassword){
  22. this.password=password;
  23. }
  24. }

在Server端使用,socket的InputStream首先被包装成GZIPInputStream,然后又被包装成ObjectInputStream,而socket的OutputStream首先被包装成GZIPOutputStream,然后又被包装成ObjectOutputStream,如下:


  1. packagecom.googlecode.garbagecan.test.socket.sample4;
  2. importjava.io.IOException;
  3. importjava.io.ObjectInputStream;
  4. importjava.io.ObjectOutputStream;
  5. importjava.net.ServerSocket;
  6. importjava.net.Socket;
  7. importjava.util.logging.Level;
  8. importjava.util.logging.Logger;
  9. importjava.util.zip.GZIPInputStream;
  10. importjava.util.zip.GZIPOutputStream;
  11. publicclassMyServer{
  12. privatefinalstaticLoggerlogger=Logger.getLogger(MyServer.class.getName());
  13. publicstaticvoidmain(String[]args)throwsIOException{
  14. ServerSocketserver=newServerSocket(10000);
  15. while(true){
  16. Socketsocket=server.accept();
  17. socket.setSoTimeout(10*1000);
  18. invoke(socket);
  19. }
  20. }
  21. privatestaticvoidinvoke(finalSocketsocket)throwsIOException{
  22. newThread(newRunnable(){
  23. publicvoidrun(){
  24. GZIPInputStreamgzipis=null;
  25. ObjectInputStreamois=null;
  26. GZIPOutputStreamgzipos=null;
  27. ObjectOutputStreamoos=null;
  28. try{
  29. gzipis=newGZIPInputStream(socket.getInputStream());
  30. ois=newObjectInputStream(gzipis);
  31. gzipos=newGZIPOutputStream(socket.getOutputStream());
  32. oos=newObjectOutputStream(gzipos);
  33. Objectobj=ois.readObject();
  34. Useruser=(User)obj;
  35. System.out.println("user:"+user.getName()+"/"+user.getPassword());
  36. user.setName(user.getName()+"_new");
  37. user.setPassword(user.getPassword()+"_new");
  38. oos.writeObject(user);
  39. oos.flush();
  40. gzipos.finish();
  41. }catch(IOExceptionex){
  42. logger.log(Level.SEVERE,null,ex);
  43. }catch(ClassNotFoundExceptionex){
  44. logger.log(Level.SEVERE,null,ex);
  45. }finally{
  46. try{
  47. ois.close();
  48. }catch(Exceptionex){}
  49. try{
  50. oos.close();
  51. }catch(Exceptionex){}
  52. try{
  53. socket.close();
  54. }catch(Exceptionex){}
  55. }
  56. }
  57. }).start();
  58. }
  59. }

Client也和Server端类似,同样要不socket的XXXStream包装成GZIPXXXStream,然后再包装成ObjectXXXStream,如下:


  1. packagecom.googlecode.garbagecan.test.socket.sample4;
  2. importjava.io.IOException;
  3. importjava.io.ObjectInputStream;
  4. importjava.io.ObjectOutputStream;
  5. importjava.net.InetSocketAddress;
  6. importjava.net.Socket;
  7. importjava.net.SocketAddress;
  8. importjava.util.logging.Level;
  9. importjava.util.logging.Logger;
  10. importjava.util.zip.GZIPInputStream;
  11. importjava.util.zip.GZIPOutputStream;
  12. publicclassMyClient{
  13. privatefinalstaticLoggerlogger=Logger.getLogger(MyClient.class.getName());
  14. publicstaticvoidmain(String[]args)throwsException{
  15. for(inti=0;i<10;i++){
  16. Socketsocket=null;
  17. GZIPOutputStreamgzipos=null;
  18. ObjectOutputStreamoos=null;
  19. GZIPInputStreamgzipis=null;
  20. ObjectInputStreamois=null;
  21. try{
  22. socket=newSocket();
  23. SocketAddresssocketAddress=newInetSocketAddress("localhost",10000);
  24. socket.connect(socketAddress,10*1000);
  25. socket.setSoTimeout(10*1000);
  26. gzipos=newGZIPOutputStream(socket.getOutputStream());
  27. oos=newObjectOutputStream(gzipos);
  28. Useruser=newUser("user_"+i,"password_"+i);
  29. oos.writeObject(user);
  30. oos.flush();
  31. gzipos.finish();
  32. gzipis=newGZIPInputStream(socket.getInputStream());
  33. ois=newObjectInputStream(gzipis);
  34. Objectobj=ois.readObject();
  35. if(obj!=null){
  36. user=(User)obj;
  37. System.out.println("user:"+user.getName()+"/"+user.getPassword());
  38. }
  39. }catch(IOExceptionex){
  40. logger.log(Level.SEVERE,null,ex);
  41. }
  42. try{
  43. oos.close();
  44. }catch(IOExceptione){
  45. }
  46. try{
  47. ois.close();
  48. }catch(IOExceptione){
  49. }
  50. try{
  51. socket.close();
  52. }catch(IOExceptione){
  53. }
  54. }
  55. }
  56. }

最后测试上面的代码,首先运行Server类,然后运行Client类,就可以分别在Server端和Client端控制台看到接收到的User对象实例了。

原文链接:http://blog.youkuaiyun.com/kongxx/article/details/7259834


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值