Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Java Socket transfer objects?

Shulou Source: shulou.com Published: 2022-06-02 21:04:35 10月04日 Update

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.

Tags: Object transfer code content more article help run good normal interest instance guy buddy that is sequence essential interface console article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Shulou Tech Info MySQL Linux