Passing Java objects (Object serialization) through socket)

Source: Internet
Author: User

This article mainly implements J: Passing Java objects through socket. The method used is object serialization. The method is to establish a C/S connection through socket, and read and write objects through objectoutputstream and objectoutputstream. The only thing to note is that the passed Java object must implement the serializable flag interface. The Code includes: Java object class, employee, Socket Client class, and server class. The main code is as follows:

Java object class:

Import java. Io .*;
Import java. util .*;

Public class employee implements serializable {

Private int employeenumber;
Private string employeename;

Employee (INT num, string name ){
Employeenumber = num;
Employeename = Name;
}

Public int getemployeenumber (){
Return employeenumber;
}

Public void setemployeenumber (INT num ){
Employeenumber = num;
}

Public String getemployeename (){
Return employeename;
}

Public void setemployeename (string name ){
Employeename = Name;
}
}

Client class:

import java.io.*;import java.net.*;public class Client {   public static void main(String[] arg) {      try {         Employee joe = new Employee(150, "Joe");         System.out.println("employeeNumber= "                            + joe .getEmployeeNumber());         System.out.println("employeeName= "                            + joe .getEmployeeName());         Socket socketConnection = new Socket("127.0.0.1", 11111);         ObjectOutputStream clientOutputStream = new            ObjectOutputStream(socketConnection.getOutputStream());         ObjectInputStream clientInputStream = new             ObjectInputStream(socketConnection.getInputStream());         clientOutputStream.writeObject(joe);         joe= (Employee)clientInputStream.readObject();         System.out.println("employeeNumber= "                            + joe .getEmployeeNumber());         System.out.println("employeeName= "                            + joe .getEmployeeName());         clientOutputStream.close();         clientInputStream.close();      } catch (Exception e) {System.out.println(e); }   }}

Server class:

Import java. Io .*;
Import java.net .*;

Public class server {

Public static void main (string [] Arg ){

Employee Employee = NULL;

Try {

Serversocket socketconnection = new serversocket (11111 );

System. Out. println ("server waiting ");

Socket pipe = socketconnection. Accept ();

Objectinputstream serverinputstream = new
Objectinputstream (pipe. getinputstream ());

Objectoutputstream serveroutputstream = new
Objectoutputstream (pipe. getoutputstream ());

Employee = (employee) serverinputstream. readobject ();

Employee. setemployeenumber (256 );
Employee. setemployeename ("John ");

Serveroutputstream. writeobject (employee );

Serverinputstream. Close ();
Serveroutputstream. Close ();

} Catch (exception e) {system. Out. println (E );
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.