国产激情久久久久影院小草_国产91高跟丝袜_99精品视频99_三级真人片在线观看

Java Socket網(wǎng)絡(luò)傳輸?shù)男蛄谢瘷C(jī)制

時(shí)間:2024-08-28 15:52:52 SUN認(rèn)證 我要投稿
  • 相關(guān)推薦

關(guān)于Java Socket網(wǎng)絡(luò)傳輸?shù)男蛄谢瘷C(jī)制

  采用Java Socket網(wǎng)絡(luò)傳輸?shù)男蛄谢瘷C(jī)制,將對(duì)象“壓扁”成二進(jìn)制字節(jié),將二進(jìn)制字節(jié)在網(wǎng)絡(luò)中傳輸;

  自定義協(xié)議,將對(duì)象用字符串描述出來(lái),將字符串用二進(jìn)制表示,在網(wǎng)絡(luò)中傳輸,在另外一邊用相反的策略解析這個(gè)字符串,重新構(gòu)造業(yè)務(wù)對(duì)象,這個(gè)方法能夠在異構(gòu)平臺(tái)中進(jìn)行傳輸而不變形,但是需要額外的編寫“壓扁”和“充氣”的代碼;

  我們這里用第一種方法:

  1.package stream.demo;

  2.import java.io.ByteArrayInputStream;

  3.import java.io.ByteArrayOutputStream;

  4.import java.io.File;

  5.import java.io.FileInputStream;

  6.import java.io.FileOutputStream;

  7.import java.io.IOException;

  8.import java.io.InputStream;

  9.import java.io.ObjectInputStream;

  10.import java.io.ObjectOutputStream;

  11.import java.io.OutputStream;

  12.import java.util.Date;

  13.public class Persistence {

  14.public static void main(String[] args) {

  15.byte[] bs = Persistence.toBytes();

  16.//在網(wǎng)絡(luò)中進(jìn)行傳輸

  17.Persistence.getBytes(bs);

  18.}

  19.public static byte[] toBytes() {

  20.Person p = new Person();

  21.p.setName("corey");

  22.p.setTall(171);

  23.p.setBirthday(new Date());

  24.p.setAddress(new Address("yiyang", "ziyang"));

  25.ByteArrayOutputStream out = new

  ByteArrayOutputStream();

  26.try {

  27.ObjectOutputStream oout = new ObjectOutputStream(out);

  28.oout.writeObject(p);

  29.} catch (IOException e) {

  30.// TODO Auto-generated catch block

  31.e.printStackTrace();

  32.}

  33.return out.toByteArray();

  34.}

  35.public static void getBytes(byte[] bs) {

  36.try {

  37.ByteArrayInputStream byteIn = new

  ByteArrayInputStream(bs);

  38.ObjectInputStream in = new ObjectInputStream(byteIn);

  39.Person p = (Person) in.readObject();

  40.System.out.println(p.getName());

  41.System.out.println(p.getTall());

  42.System.out.println(p.getBirthday());

  43.System.out.println(p.getAddress().getCity());

  44.System.out.print(p.getAddress().getStreet());

  45.} catch (Exception e) {

  46.// TODO Auto-generated catch block

  47.e.printStackTrace();

  48.}

  49.}

  50.}

  其中服務(wù)端代碼片段為:

  51.in = this.getRequestSocket().getInputStream();

  52.out = this.getRequestSocket().getOutputStream();

  53.byte[] bs = Persistence.toBytes();

  54.System.out.println("發(fā)送數(shù)字長(zhǎng)度:"+bs.length);

  55.out.write(bs);

  56.this.getRequestSocket().close();

  57.客戶端代碼片段為:

  58.InputStream in = request.getInputStream();

  59.byte[] bin = new byte[200];

  60.int length = 0;

  61.while ((length = in.read(bin)) != -1) {

  62.System.out.println("length:" + length);

  63.Persistence.getBytes(bin);

  64.}

【Java Socket網(wǎng)絡(luò)傳輸?shù)男蛄谢瘷C(jī)制】相關(guān)文章:

Java語(yǔ)言的特點(diǎn)和實(shí)現(xiàn)機(jī)制10-01

PHP socket的配置08-04

技術(shù)知識(shí)學(xué)習(xí):網(wǎng)絡(luò)傳輸介質(zhì)10-03

JAVA認(rèn)證基礎(chǔ)知識(shí):基于反射機(jī)制的服務(wù)代理調(diào)用09-20

PHP中如何使用socket進(jìn)行通信08-21

2016年JAVA認(rèn)證基礎(chǔ)知識(shí):基于反射機(jī)制的服務(wù)代理調(diào)用08-21

Java與Java web的區(qū)別08-10

簡(jiǎn)單分析PHP中序列化用法介紹08-15

電腦文件怎么傳輸?shù)絠Pad07-30

java習(xí)題及答案10-25