关于将序列化的JAVA类对象在Socket传输

引用

[*]
引用
JAVA中的对象序列化是用来讲java的类对象实例编码成字节流在传输层进行传输【这个过程可以被称作序列化】,并且在服务端接收到序列化之后的对象进行反序列化从字节流编码中重新构建对象【这个过程可以被称为反序列化】。可以这么说JAVA序列化技术为java远程通信提供了标准的线路级对象表示法,也为JAVABEANS组件结构提供了标准的持久化数据格式,【这句话引自effective java】。
[*]
[*]下面的代码例子很简单,就是一个简单的序列化与反序列化的过程。主要是将一个JAVA类对象序列化后传输到服务端,在服务端进行反序列化重构类对象。
持久化类:Persistence.java
package com.test.serializable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class Persistence implements Serializable {
	
	private Person person ;
	
	public Persistence(){
		person = new Person();
		person.setNameString("Jack");
	    person.setSex("M");
	    person.setBirthday("2013");
	    person.setStreet("shangdi");
	    person.setAddress("beijing");
	}
	
	public byte[] toBytes() {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			ObjectOutputStream obout = new ObjectOutputStream(out);
			obout.writeObject(person);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return out.toByteArray();
	}

	public Person readBytes(byte[] bs) {
		Person person = null;
		try {
			ByteArrayInputStream byteIn = new ByteArrayInputStream(bs);
			ObjectInputStream in = new ObjectInputStream(byteIn);
			person = (Person) in.readObject();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return person;
	}

	public class Person implements Serializable {
		String nameString;
		String sex;
		String birthday;
		String address;
		String street;

		public String getNameString() {
			return nameString;
		}

		public void setNameString(String nameString) {
			this.nameString = nameString;
		}

		public String getSex() {
			return sex;
		}

		public void setSex(String sex) {
			this.sex = sex;
		}

		public String getBirthday() {
			return birthday;
		}

		public void setBirthday(String birthday) {
			this.birthday = birthday;
		}

		public String getAddress() {
			return address;
		}

		public void setAddress(String address) {
			this.address = address;
		}

		public String getStreet() {
			return street;
		}

		public void setStreet(String street) {
			this.street = street;
		}

	}
}



客户端类:Client.java
package com.test.serializable;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
	private String ipAddress = "127.0.0.1";
	private int port = 9000;
	private Persistence persistence ;
	Socket socket;
	

	public Client() {
		persistence = new Persistence();
		try {
			socket = new Socket(ipAddress, port);
		} catch (UnknownHostException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void outToServer() throws IOException {
		System.out.println("-----Client Begin Send Data-----");
		OutputStream outputStream = socket.getOutputStream();
		byte b[] = new byte[2048];
		b = persistence.toBytes();
		outputStream.write(b);
		outputStream.close();
	}
	public static void main(String args[]) throws IOException{
		Client client = new Client();
		client.outToServer();
	}
}


服务端类:Server.java

package com.test.serializable;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.bes.serializable.Persistence.Person;

public class Server {

	ServerSocket serverSocket;
	BufferedReader bufferedReader = null;
	Socket socket;

	public Server(){
		try {
			serverSocket = new ServerSocket(9000);
			System.out.println("Waiting For Accept Client ");
			socket = serverSocket.accept();
			System.out.println("Accepted Client ");
}
catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void readFromClient(){
		try {
			System.out.println("------Begin Received From Client------");
			Persistence persistence = new Persistence();
			InputStream inputStream = socket.getInputStream();
			byte b [] = new byte[2048];
			inputStream.read(b);
		   Person object =	persistence.readBytes(b);
		   
			System.out.println("Read From Client Data is :" + object.getAddress());
			System.out.println("Read From Client Data is :" + object.getBirthday());
			System.out.println("Read From Client Data is :" + object.getNameString());
			System.out.println("Read From Client Data is :" + object.getStreet());
			System.out.println("Read From Client Data is :" + object.getSex());
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String args[]){
		Server s = new Server();
		s.readFromClient();
	}
}

你可能感兴趣的:(java,socket)