Search code examples
javasocketsobjectinputstream

java.io.StreamCorruptedException: invalid type code: 68


I have a client and server that have a ObjectInputStream in and out. When I use it for the client to send data it does. But when I try to the same object type back it gives me the above error.

Server: 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;
      }

Command to trigger the send:

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

Section to receive the object:

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

Client: Socket connection:

           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");
       }

Send the object:

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

Receive plr object:

    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 object being sent:

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;

}

Now I know I'm new to the object sockets so I'm sure I'm missing this.


Solution

  • You need to use the same type for input/output streams in both ends (server, client). So, change your code to:

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

    Remove the other in & out

    and

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

    Remove the other in & out