Java Socket通信要如何才能把序列化和反序列化做好呢?这个问题需要我们不断的进行学习有关代码,在以往的使用中有很多的经验值得我们学习。下面我们就详细的学习下有关的代码。#t#
在Java Socket通信中,我们要在客户端和服务端中传输数据,传输的数据有各种类型,int,short,long以及String,甚至是自定义的各种Object,我们如何能够保证在服务端和客户端之间的数据能够使得两端都能理解,这就是所谓的“应用协议”;在通信的“管道”中,流的都是二进制数据,所以,双方如何约定好“解析规则”,则就必须要求通信双方都预先制定好协议;
序列化和反序列化
在Java Socket通信中,有一个借口Serializable接口,这个接口不需要实现实现任何方法,只是一个标识,Java 序列化技术可以使你将一个对象的状态写入一个Byte 流里,并且可以从其它地方把该Byte 流里的数据读出来。重新构造一个相同的对象。这种机制允许你将对象通过网络进行传播,并可以随时把对象持久化到数据库、文件等系统里。
代码如下:
- Person:
- import java.io.Serializable;
- import java.util.Date;
- public class Person implements Serializable {
- private String name;
- private int tall;
- private transient Date birthday;
- private Address address;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getTall() {
- return tall;
- }
- public void setTall(int tall) {
- this.tall = tall;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Address getAddress() {
- return address;
- }
- public void setAddress(Address address) {
- this.address = address;
- }
- }
- Address:
- package stream.demo;
- import java.io.Serializable;
- public class Address implements Serializable{
- private String city;
- private String street;
- public Address() {
- }
- public Address(String city, String street) {
- this.city = city;
- this.street = street;
- }
- public String getCity() {
- return city;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public String getStreet() {
- return street;
- }
- public void setStreet(String street) {
- this.street = street;
- }
- }
- Person:
- import java.io.Serializable;
- import java.util.Date;
- public class Person implements Serializable {
- private String name;
- private int tall;
- private transient Date birthday;
- private Address address;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getTall() {
- return tall;
- }
- public void setTall(int tall) {
- this.tall = tall;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Address getAddress() {
- return address;
- }
- public void setAddress(Address address) {
- this.address = address;
- }
- }
- Address:
- package stream.demo;
- import java.io.Serializable;
- public class Address implements Serializable{
- private String city;
- private String street;
- public Address() {
- }
- public Address(String city, String street) {
- this.city = city;
- this.street = street;
- }
- public String getCity() {
- return city;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public String getStreet() {
- return street;
- }
- public void setStreet(String street) {
- this.street = street;
- }
以上就是对Java Socket通信的详细介绍,希望大家找到自己问题的答案。
本文标题:JavaSocket通信的序列化和反序列化代码介绍
文章起源:http://www.shufengxianlan.com/qtweb/news24/384524.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联