In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to transfer compressed objects in Java Socket. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The last article talked about using Java Socket to transfer objects, but in some cases, such as the network environment is not good or the object is relatively large, you need to compress the data objects and then transfer them. At this time, you need to compress these object streams. At this time, GZIPInputStream and GZIPOutputStream can deal with the InputStream and OutputStream of socket.
You still need a simple Java object that implements the java.io.Serializable interface:
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;}}
On the server side, the InputStream of socket is first packaged as GZIPInputStream and then as ObjectInputStream, while the OutputStream of socket is first packaged as GZIPOutputStream and then packaged as ObjectOutputStream, as follows:
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 is also similar to the server side. Similarly, the XXXStream that is not socket should be packaged as GZIPXXXStream, and then packaged as ObjectXXXStream, as follows:
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; iTunes +) {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);} try {oos.close ();} catch (IOException e) {} try {ois.close () } catch (IOException e) {} try {socket.close ();} catch (IOException e) {}}
To test the above code, first run the Server class, and then run the Client class, and you can see the received User object instances on the server and client consoles, respectively.
On how to transfer compressed objects in Java Socket to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.