Java Socket practice 3 transfer object

               

Address: http://blog.csdn.net/kongxx/article/details/7259827

Java Socket one of the actual combat single thread communication

Java Socket practice two multithreaded communication

The previous two articles introduced how to establish Java Socket communication. This article described how to use Java Socket to transfer objects.

First of all, you need a common object class. Because you need to serialize this object for transmission on the network, it is necessary to implement the java.io.Serializable interface. Enter:

package com.googlecode.garbagecan.test.socket.sample3;public class User implements java.io.Serializable private static final long serialVersionUID = 1Lprivate 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 in the code to receive and send InputStream and OutputStream in the socket, and then they are converted into 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(); }}
Similar to the Server side, the Client also uses ObjectOutputStream and ObjectInputStream for processing, 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; i++) {   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) {}   }  } }}
Finally, test the above code, first run the Server class, then the Client class, and you can see the received User object instances in the Server and Client console respectively.

           

Keywords: socket Java network

Added by onlinegamesnz on Sun, 24 Nov 2019 19:28:46 +0200