JAVA 通过网络传输对象(对象序列化)简单示例

本文介绍了一个使用Java实现的简单示例,演示了如何通过Socket编程在网络中传输自定义对象。具体步骤包括:创建实现了Serializable接口的Student类;设置Socket服务器以监听特定端口并接收客户端发送的对象;客户端实例化Student对象并将其序列化后发送到服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,要传输的Student类(实现Serializable接口):

public class Student implements Serializable{

	private static final long serialVersionUID = 8683452581334592189L;
	private String name;
	private int age;
	private int score;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "name:" + name + " age:" + age + " score:" + score;
	}
	
	
	
}

Socket服务器类核心方法:


	public static void openObjectServer(){
		

		ServerSocket ss = null;
		try {
			ss = new ServerSocket(1111);
			while(true){
			final Socket socket = ss.accept();
			new Runnable(){

				public void run() {
					try {
					InputStream is = socket.getInputStream();
					OutputStream os = socket.getOutputStream();
					os.write("欢迎连接 服务器 一号!".getBytes());
					
					ObjectInputStream ois = new ObjectInputStream(is);
					Object object = ois.readObject();
					//打印对象
				
					System.out.println(object);	
					//关闭socket
					socket.close();
					}catch(Exception e){
						e.printStackTrace();
						
						
					}finally{
						if(socket != null )
							try {
								socket.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
					}
				}}.run();
		
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			System.out.println("服务器关闭连接!");
			try {
				if(ss != null)
				ss.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


说明:该服务器方法可以接受多个客户端连接,一般网络服务器编程核心思想应该都是这样,开启后,支持多线程处理多个连接请求,互不影响。


Socket客户端类:


public class ClientSocketClass {

	public static void main(String[] args){
		Socket socket = null;

		try{
		socket = new Socket(InetAddress.getByName("127.0.0.1"),1111);
		OutputStream os = socket.getOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(os);
		
		Student student = new Student();
		student.setAge(20);
		student.setName("wjw");
		student.setScore(100);
		
		oos.writeObject(student);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
		
			try {
				if(socket != null)
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


说明:将对象序列化,通过网络传输给服务器。


结果:在服务器端可以成功接收对象并打印。


如有错误,欢迎指正

end



评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值