| 订阅 | 在线投稿
分享
 
 
 

通过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- 王朝网络 版权所有