| 導購 | 订阅 | 在线投稿
分享
 
 
 

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

來源:互聯網網民  2008-05-31 12:10:36  評論

這篇文章主要實現的是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);

}

}

}

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
  這篇文章主要實現的是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); } } }
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 
 熱帖排行
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有