Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java Socket transfer objects?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to transfer the object of Java Socket, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The first two articles described how to set up Java Socket communications, and this one describes how to use Java Socket to transfer objects.

First of all, you need a normal object class, and since you need to serialize this object for transmission over the network, it is necessary to implement the java.io.Serializable interface, as follows:

Package com.googlecode.garbagecan.test.socket.sample3; 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;}}

For the code on the server side, ObjectInputStream and ObjectOutputStream are used to receive and send InputStream and OutputStream in socket, respectively, and then converted to Java objects, as follows:

Package com.googlecode.garbagecan.test.socket.sample3; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; 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 (); invoke (socket);}} private static void invoke (final Socket socket) throws IOException {new Thread (new Runnable () {public void run () {ObjectInputStream is = null; ObjectOutputStream os = null Try {is = new ObjectInputStream (new BufferedInputStream (socket.getInputStream (); os = new ObjectOutputStream (socket.getOutputStream ()); Object obj = is.readObject (); User user = (User) obj System.out.println ("user:" + user.getName () + "/" + user.getPassword ()); user.setName (user.getName () + "_ new"); user.setPassword (user.getPassword () + "_ new"); os.writeObject (user); os.flush () } catch (IOException ex) {logger.log (Level.SEVERE, null, ex);} catch (ClassNotFoundException ex) {logger.log (Level.SEVERE, null, ex);} finally {try {is.close () } catch (Exception ex) {} try {os.close ();} catch (Exception ex) {} try {socket.close () } catch (Exception ex) {}}) .start ();}}

Client is also similar to the server side, also using ObjectOutputStream and ObjectInputStream to handle, as follows:

Package com.googlecode.garbagecan.test.socket.sample3; import java.io.BufferedInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; 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 < 100; iTunes +) {Socket socket = null; ObjectOutputStream os = null; ObjectInputStream is = null; try {socket = new Socket ("localhost", 10000) Os = new ObjectOutputStream (socket.getOutputStream ()); User user = new User ("user_" + I, "password_" + I); os.writeObject (user); os.flush (); is = new ObjectInputStream (new BufferedInputStream (socket.getInputStream () Object obj = is.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 {is.close ();} catch (Exception ex) {} try {os.close () } catch (Exception ex) {} try {socket.close ();} catch (Exception ex) {}}

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 the object Java Socket to share here, I hope that 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report