对象序列化在网络通信中的运用

  1. 都知道对象序列化这个知识,可到底有什么用处?
    a当你想把的内存中的对象保存到一个文件中或者数据库中时候;
    b当你想用套接字在网络上传送对象的时候;
    c当你想通过RMI传输对象的时候
    2.今天通过实例来看看对象序列化在网络通信的运用(socket)
    3.对象继承序列化接口

import java.io.Serializable;

public class TestObject implements Serializable{
     /**
      *
      */
     private static final long serialVersionUID = 1L;

     private String name;
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
    }

4.实现服务器端代码

package com;
import java.net.ServerSocket;
import java.net.Socket;
public class SendServer extends java.lang.Thread {
 private boolean OutServer = false;
 private ServerSocket server;
 private final int ServerPort = 8765;
 public SendServer() {
  try {
   server = new ServerSocket(ServerPort);
   System.out.println("The server is running...");
  } catch (java.io.IOException e) {
   System.out.println("Socket start-up error!");
   System.out.println("IOException :" + e.toString());
  }
 }
 public void run() {
  Socket socket;
  java.io.ObjectInputStream in;
  while (!OutServer) {
   socket = null;
   try {
    synchronized (server) {
     socket = server.accept();
    }
    System.out.println("Built a connection: InetAddress = "
      + socket.getInetAddress());
    socket.setSoTimeout(15000);
    in = new java.io.ObjectInputStream(socket.getInputStream());
    TestObject data = (TestObject) in.readObject();
    System.out.println("The value received:" + data.getName());
    in.close();
    in = null;
    socket.close();
   } catch (java.io.IOException e) {
    System.out.println("Socket connection error!");
    System.out.println("IOException :" + e.toString());
   } catch (java.lang.ClassNotFoundException e) {
    System.out.println("ClassNotFoundException :" + e.toString());
   }
  }
 }
 public static void main(String args[]) {
  (new SendServer()).start();
 }
}

5.客户端代码


import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class SendClient {
 private String address = "127.0.0.1";
 private int port = 8765;
 public SendClient() {
  // Prepare the data need to transmit
  TestObject data = new TestObject();
  data.setName("赵丽颖");     //给对象设置状态
  Socket client = new Socket();
  InetSocketAddress isa = new InetSocketAddress(this.address, this.port);
  try {
   client.connect(isa, 10000);
   ObjectOutputStream out = new ObjectOutputStream(
     client.getOutputStream());
   // send object
   out.writeObject(data);   //把序列化的数据写到输出流中
   out.flush();
   out.close();
   out = null;
   data = null;
   client.close();
   client = null;
  } catch (java.io.IOException e) {
   System.out.println("Socket connection error!");
   System.out.println("IOException :" + e.toString());
  }
 }
 public static void main(String args[]) {
  new SendClient();
 }
} 

6.效果展示
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值