zoukankan      html  css  js  c++  java
  • Socket与Object流

    java在Client和Servet端进行自定义对象传输时注意要点

    1. 必须在客户端和服务端各自创建一个被序列化的同的类,类中的字段也必须相同,否则有些字段会访问不到
    2. 如果被序列化的类的成员变量中包含其他了类的对象,则该类也需要被序列化,否则可能会出现StreamCorropedException异常
    3. 每一个Object流对象只能创建一个,切记不能在while循环里面创建类似new ObjectOutputStream(Socket.getOutputStream())的语句,否则会抛出异常。Object流是配套使用的,即如果你在服务端创建了一个输出流,那么你在客户端必须创建一个输入流与之对应起来,这是因为Object流在使用输出流时,会先写入一个header,在使用输入流读取时会先读取header,如果不一致,则会出现异常 invade type code:...。
    4.  1 服务端代码:
       2 package webTest;
       3 
       4 import java.io.ObjectInputStream;
       5 import java.io.ObjectOutputStream;
       6 import java.net.ServerSocket;
       7 import java.net.Socket;
       8 
       9 import Public.Message;
      10 
      11 public class Server {
      12 
      13     public static void main(String[] args) throws Exception {
      14 
      15         ServerSocket ssocket  = new ServerSocket(9999);
      16         
      17         Socket socket = ssocket.accept();
      18         while(true){
      19             ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());//这个流对象创建的顺序应该和客户端的相反,否则会阻塞,因为ObjectInputStream构造函数是阻塞的。像该代码这样就会阻塞
      20             ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
      21             
      22             System.out.println("objOut+"+objOut);
      23             Message message = new Message();
      24             message.setAge(18);
      25             
      26             objOut.writeObject(message);
      27             Message message1 = new Message();
      28             message1 = (Message) objIn.readObject();
      29             System.out.println("message+" + message1.getAge());
      30 
      31 
      32         }
      33         
      34         
      35         
      36     }
      37 
      38 }
      39 
      40 客户端代码:
      41 package TCP;
      42 
      43 
      44 
      45 import java.io.ObjectInputStream;
      46 import java.io.ObjectOutputStream;
      47 import java.net.Socket;
      48 
      49 import Public.Message;
      50 
      51 
      52 
      53 
      54 public class client {
      55 
      56     public static void main(String[] args  ) throws Exception {
      57         Socket socket = new Socket("127.0.0.1",9999);
      58 
      59         while(true){
      60             ObjectInputStream objIn= new ObjectInputStream(socket.getInputStream());
      61             ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
      62             
      63             Message message1 = new Message();
      64             message1.setAge(181);
      65             objOut.writeObject(message1);
      66             
      67             System.out.println("objIn+"+objIn);
      68             Message message = new Message();
      69             message = (Message) objIn.readObject();
      70             System.out.println("message+" + message.getAge());
      71         }
      72     }
      73 }

    5. 必须把这两个类分别放在客户端与服务端具有相同包名的包下面,否则出现ClassNotFoundException异常或者ClassCastException异常
    6. 如图

      

     

     

     

     

     

     

     

     

     

    客户端代码:

     1 package TCP;
     2 
     3  
     4 
     5 import java.io.ObjectInputStream;
     6 import java.io.ObjectOutputStream;
     7 import java.net.Socket;
     8 
     9 import Public.Message;
    10 
    11  
    12 
    13 
    14 public class client {
    15 
    16 public static void main(String[] args ) throws Exception {
    17 Socket socket = new Socket("127.0.0.1",9999);
    18 
    19 ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
    20 
    21 ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());
    22 
    23 //    objOut.writeObject("客户端来消息了");
    24 
    25 Message message = new Message();
    26 message.setAge(18);
    27 
    28 objOut.writeObject(message);
    29 
    30 
    31 }
    32 
    33 }

      

    服务端代码:

      

     1 package webTest;
     2 
     3 import java.io.ObjectInputStream;
     4 import java.io.ObjectOutputStream;
     5 import java.net.ServerSocket;
     6 import java.net.Socket;
     7 
     8 import Public.Message;
     9 
    10 public class Server {
    11 
    12 public static void main(String[] args) throws Exception {
    13 
    14 ServerSocket ssocket = new ServerSocket(9999);
    15 
    16 Socket socket = ssocket.accept();
    17 
    18 ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());
    19 
    20 ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
    21 
    22 objOut.writeObject("服务端来消息了");
    23 
    24 Message message = new Message();
    25 message = (Message) objIn.readObject();
    26 System.out.println("message+"+message.getAge());
    27 
    28 
    29 String str = (String)objIn.readObject();
    30 
    31 System.out.println("str+"+str);
    32 
    33 
    34 
    35 }
    36 
    37 }

    消息类:

     1 package Public;
     2 
     3 import java.io.Serializable;
     4 
     5 public class Message implements Serializable{
     6 /**
     7 * 
     8 */
     9 private static final long serialVersionUID = 8625279621236358825L;//服务端与客户端必须完全一致
    10 
    11 private Postion pos;//该类在服务端与客户端都必须存在且完全一致
    12 private int age;
    13 
    14 public int getAge() {
    15 return age;
    16 }
    17 
    18 public void setAge(int age) {
    19 this.age = age;
    20 }
    21 
    22 }

     

  • 相关阅读:
    S2-001 远程代码执行漏洞
    Struts2 devMode远程代码执行漏洞检测利用
    CVE-2017-12615(Tomcat PUT方法任意写文件漏洞)
    Medium | LeetCode 343 | 剑指 Offer 14- I. 剪绳子 | 动态规划
    Medium | 剑指 Offer 38. 字符串的排列 | 回溯
    Medium | 剑指 Offer 60. n个骰子的点数 | 动态规划
    Medium | 剑指 Offer 46. 把数字翻译成字符串 | 回溯
    Medium | 剑指 Offer 59
    Hard | LeetCode 239 | 剑指 Offer 59
    Medium | 剑指 Offer 35. 复杂链表的复制
  • 原文地址:https://www.cnblogs.com/studyCenter/p/6361148.html
Copyright © 2011-2022 走看看