【问题标题】:java.io.StreamCorruptedException: invalid type code: 68java.io.StreamCorruptedException:无效类型代码:68
【发布时间】:2014-09-18 23:30:56
【问题描述】:

我有一个有 ObjectInputStream 输入和输出的客户端和服务器。当我将它用于客户端发送数据时,它确实如此。但是当我尝试返回相同的对象类型时,它给了我上述错误。

服务器: ClientWorker:

public void run(){
      String line = null;
      try{
          oout = new ObjectOutputStream(client.getOutputStream());  
          oin = new ObjectInputStream(client.getInputStream()); 
          in = new BufferedReader(new InputStreamReader(client.getInputStream()));
          out = new PrintWriter(client.getOutputStream(), true);
      } catch (IOException e) {
          System.out.println("in or out failed");
          Mud.conn[connnmb]=0;
          Mud.sock[connnmb]=null;
      }

触发发送的命令:

    else if (line.startsWith("stats")){
        Debug.disPlr(plr);
        Mud.sock[conn].oout.writeObject(plr);
    }

接收对象的部分:

                      newguy=in.readLine();
                  if (newguy.equals("¥done")){
                      plr=(Player)oin.readObject();
                      System.out.println(">"+plr.getPassword());
                      out.println("¥ok");

客户: 套接字连接:

           try{
         socket = new Socket("localhost", 42024);
         oout = new ObjectOutputStream(socket.getOutputStream());  
         oin = new ObjectInputStream(socket.getInputStream());
         out = new PrintWriter(socket.getOutputStream(), 
                     true);
         in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
         connected=1;
         //Thread obsock=new Thread(new objectSocket());
         //obsock.start();
       } catch (UnknownHostException e) {
           Styles.writeConsole("Unknown host");
       } catch  (IOException e) {
           Styles.writeConsole("No I/O");
       }

发送对象:

}else if (line.equals("¥plr")){
    out.println("¥done");
    oout.writeObject(plr);

接收plr对象:

    if (e.getSource()==btnchar){
    out.println("stats");
    try {
        Object read=oin.readObject();
        plr=(Player)read;

    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ClassNotFoundException e1) {
        System.out.println("shit");
        e1.printStackTrace();
    }
    cd=new ClassDisplay();

正在发送的玩家对象:

package net.bearfather.Mud;

import java.io.Serializable;
import java.util.ArrayList;

public class Player implements Serializable{
    private static final long serialVersionUID = 1L;
    private int conn=0;
    private Mob mob=null;
    private Boolean combat=false;
    private Boolean blessed=false;
    private Spell[] activesp=new Spell[10];
    private int combattype=0;
    private int combatspell=0;
    private int rec=0;
    private String name="blank";
    private String password="blank";
    private int clss=0;
    private String race="none";
    private ArrayList<String> inv=new ArrayList<String>();
    private int lvl;
    private int exp=0;
    private int cash=0;
    private int lastbaught=0;
    private ArrayList<String> abils=new ArrayList<String>();
    private ArrayList<String> tabils=new ArrayList<String>();
    private ArrayList<String> eabils=new ArrayList<String>();
    public ArrayList<String> rtn=new ArrayList<String>();
    private int room=1;
    private int map=1;
    private int mana=0;
    private int curmana=0;
    private int manargn=0;
    //hitpoints
    private int maxHps=10;
    private int curmaxHps=10;
    private int curHps=10;
    //defenses and attacks
    private int ac=0;
    private int cac=0;
    private int dr=0;
    private int cdr=0;
    private int mr=0;
    private int cmr=0;
    private int att=0;
    private int catt=0;
    private int sc=0;
    private int csc=0;
    private int minhit=0;
    private int cminhit=0;
    private int maxhit=3;
    private int cmaxhit=0;
    //stats
    private int str=0;
    private int dex=0;
    private int con=0;
    private int wis=0;
    private int itl=0;
    private int chr=0;

}

现在我知道我是对象套接字的新手,所以我确定我错过了这个。

【问题讨论】:

    标签: java sockets objectinputstream


    【解决方案1】:

    您需要对两端(服务器、客户端)的输入/输出流使用相同的类型。因此,将您的代码更改为:

    oout = new ObjectOutputStream(client.getOutputStream());  
    oin = new ObjectInputStream(client.getInputStream()); 
    // REMOVE THIS LINE in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    // REMOVE THIS LINE out = new PrintWriter(client.getOutputStream(), true);
    

    删除另一个进出

    oout = new ObjectOutputStream(socket.getOutputStream());  
    oin = new ObjectInputStream(socket.getInputStream());
    // REMOVE THIS LINE out = new PrintWriter(socket.getOutputStream(), true);
    // REMOVE THIS LINE in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    

    删除另一个进出

    【讨论】:

    • 但是对于普通字符串,我一直在使用 in 和 out 。我也应该只使用一个字符串吗?就像我说的我很新。
    • 你不能“过度使用”它。每个套接字只有两个流。一个进去,一个出去。您使用一种类型来处理它们(例如 DataInputStream/DataOutputStream、ObjectInputStream/ObjectOutputStream 等)。对于您的情况,您甚至可以对 String 对象使用 ObjectInputStream/ObjectOutputStream
    • 谢谢!那是我的问题。我现在已经了解了更多关于套接字的知识,我会支持你,但我没有排名:(
    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多