2014-09-18 52 views
0

我有一個客戶端和服務器有一個ObjectInputStream進出。當我使用它爲客戶端發送數據時。但是當我嘗試使用相同的對象類型時,它會給我上面的錯誤。java.io.StreamCorruptedException:無效的類型代碼:68

服務器: 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"); 

客戶: Socket連接:

  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(); 

Player對象:

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; 

}

現在我知道我米新的對象套接字,所以我敢肯定我錯過了這一點。

回答

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())); 

在&出

+0

取出另一個但我一直在使用和退出在兩個正常的字符串。我是否應該只用一個字符串呢?就像我說的,我很新。 – BearFather 2014-09-19 01:40:20

+0

你不能「過度使用」它。每個插座只有兩個流。一個進去,另一個出去。你使用一種類型來處理它們(例如DataInputStream/DataOutputStream,ObjectInputStream/ObjectOutputStream等等)。對於你的情況,你甚至可以使用ObjectInputStream/ObjectOutputStream對於String對象 – Multithreader 2014-09-19 02:20:40

+0

謝謝!這是我的問題。我現在已經學會了一些關於套接字,我會upvote你,但我沒有排名:( – BearFather 2014-09-19 05:40:09

相關問題