通過socket串連傳遞java對象(對象序列化)

來源:互聯網
上載者:User

這篇文章主要實現的是j:通過socket傳遞Java對象。採用的方法就是對象序列化。方法是:通過socket建立c/s串連;通過ObjectOutputStream,ObjectOutputStream 讀寫對象。唯一需要留意的是傳遞的java 對象需要實現Serializable標記介面。程式碼封裝括:java對象類,Employee; socket client類;server類。主要代碼如下:

java對象類:

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類:

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類:

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);
      }
   }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.