Java Socket实现传输压缩对象的方法示例
程序员文章站
2023-12-12 15:03:40
本文实例讲述了java socket实现传输压缩对象的方法。分享给大家供大家参考,具体如下:
前面文章《java socket实现的传输对象功能示例》说到了用java s...
本文实例讲述了java socket实现传输压缩对象的方法。分享给大家供大家参考,具体如下:
前面文章《java socket实现的传输对象功能示例》说到了用java socket来传输对象,但是在有些情况下比如网络环境不好或者对象比较大的情况下需要把数据对象进行压缩然后在传输,此时就需要压缩这些对象流,此时就可以gzipinputstream和gzipoutputstream来处理一下socket的inputstream和outputstream。
仍然需要一个实现了java.io.serializable接口的简单java对象
package com.googlecode.garbagecan.test.socket.sample4; public class user implements java.io.serializable { private static final long serialversionuid = 1l; private string name; private string password; public user() { } public user(string name, string password) { this.name = name; this.password = password; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } }
在server端使用,socket的inputstream首先被包装成gzipinputstream,然后又被包装成objectinputstream,而socket的outputstream首先被包装成gzipoutputstream,然后又被包装成objectoutputstream,如下:
package com.googlecode.garbagecan.test.socket.sample4; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.net.serversocket; import java.net.socket; import java.util.logging.level; import java.util.logging.logger; import java.util.zip.gzipinputstream; import java.util.zip.gzipoutputstream; public class myserver { private final static logger logger = logger.getlogger(myserver.class.getname()); public static void main(string[] args) throws ioexception { serversocket server = new serversocket(10000); while (true) { socket socket = server.accept(); socket.setsotimeout(10 * 1000); invoke(socket); } } private static void invoke(final socket socket) throws ioexception { new thread(new runnable() { public void run() { gzipinputstream gzipis = null; objectinputstream ois = null; gzipoutputstream gzipos = null; objectoutputstream oos = null; try { gzipis = new gzipinputstream(socket.getinputstream()); ois = new objectinputstream(gzipis); gzipos = new gzipoutputstream(socket.getoutputstream()); oos = new objectoutputstream(gzipos); object obj = ois.readobject(); user user = (user)obj; system.out.println("user: " + user.getname() + "/" + user.getpassword()); user.setname(user.getname() + "_new"); user.setpassword(user.getpassword() + "_new"); oos.writeobject(user); oos.flush(); gzipos.finish(); } catch (ioexception ex) { logger.log(level.severe, null, ex); } catch(classnotfoundexception ex) { logger.log(level.severe, null, ex); } finally { try { ois.close(); } catch(exception ex) {} try { oos.close(); } catch(exception ex) {} try { socket.close(); } catch(exception ex) {} } } }).start(); } }
client也和server端类似,同样要不socket的xxxstream包装成gzipxxxstream,然后再包装成objectxxxstream,如下:
package com.googlecode.garbagecan.test.socket.sample4; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.net.inetsocketaddress; import java.net.socket; import java.net.socketaddress; import java.util.logging.level; import java.util.logging.logger; import java.util.zip.gzipinputstream; import java.util.zip.gzipoutputstream; public class myclient { private final static logger logger = logger.getlogger(myclient.class.getname()); public static void main(string[] args) throws exception { for (int i = 0; i < 10; i++) { socket socket = null; gzipoutputstream gzipos = null; objectoutputstream oos = null; gzipinputstream gzipis = null; objectinputstream ois = null; try { socket = new socket(); socketaddress socketaddress = new inetsocketaddress("localhost", 10000); socket.connect(socketaddress, 10 * 1000); socket.setsotimeout(10 * 1000); gzipos = new gzipoutputstream(socket.getoutputstream()); oos = new objectoutputstream(gzipos); user user = new user("user_" + i, "password_" + i); oos.writeobject(user); oos.flush(); gzipos.finish(); gzipis = new gzipinputstream(socket.getinputstream()); ois = new objectinputstream(gzipis); object obj = ois.readobject(); if (obj != null) { user = (user)obj; system.out.println("user: " + user.getname() + "/" + user.getpassword()); } } catch(ioexception ex) { logger.log(level.severe, null, ex); } finally { try { ois.close(); } catch(exception ex) {} try { oos.close(); } catch(exception ex) {} try { socket.close(); } catch(exception ex) {} } } } }
最后测试上面的代码,首先运行server类,然后运行client类,就可以分别在server端和client端控制台看到接收到的user对象实例了。
更多关于java相关内容感兴趣的读者可查看本站专题:《java socket编程技巧总结》、《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。