Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-10-2009, 10:39 PM
Member
 
Join Date: Dec 2008
Posts: 5
Rep Power: 0
Wassa is on a distinguished road
Default StreamCorruptedException and Casting troubles
It seems that every now and then, my project has been throwing exceptions when my client has received an object from the client.

I have no idea what is causing it as the stack trace leads from the objectinputstream.receive() into the java API.

The exceptions I am getting are either 'StreamCorruptedExceptions' or Casting errors where it attempts to cast one object from within the tableUpdate onto another (see class below), but I have no code which is doing this and I think it is related to the corruptedstream as they both started cropping up at the same time.

Does anyone know what may be causing it?

Example Exception:
Code:
java.io.StreamCorruptedException: invalid handle value: 7371007E

        at java.io.ObjectInputStream.readHandle(ObjectInputStream.java:1433)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1309)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
        at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1667)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
        at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1667)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
        at common.network.IOSocket.receive(IOSocket.java:59)
        at client.execute.ClientThread.run(ClientThread.java:34)
        at java.lang.Thread.run(Thread.java:619)
The Object Which Is Being Sent: (all objects within are also Serializable)
Code:
package server.table.updates;

import common.cards.Card;
import java.io.Serializable;
import server.game.variables.BlindLevel;
import server.game.Game;
import server.game.variables.PokerBreed;
import server.playablehand.HandStage;
import server.table.player.Player;

public class TableUpdate implements Serializable {
    private Player[] players;
    private Card communityCards[] = new Card[]{ null, null, null, null, null};
    private int dealerPosition;
    private BlindLevel blinds;
    private int id;
    private int pot = -1;
    private String name;
    private PokerBreed breed;
    private int playerTurn = -1;
    private UpdateType type;
    private String displayText = "";
    
    /**
     * Creates a new instance of TableUpdate
     */
    public TableUpdate(Game game, UpdateType type) {
        this.type = type;
        init(game);
        
    }
    
    private void init(Game game){
        this.breed = game.getState().getBreed();
        this.id = game.getId();
        this.name = game.getName().toString();
        this.blinds = game.getState().getTable().getBlinds();
        this.dealerPosition = game.getState().getTable().getDealerPosition();
        this.players = game.getState().getTable().getPlayers();
        
        
        
        // if the game is not paused there will not be a hand being played
        if (!this.type.equals(UpdateType.SHOWPAUSEDGAME)){
            this.communityCards = game.getHand().getCommunityCards();
            this.playerTurn = game.getHand().getHandPlayer().getPlayerTurn();
            this.pot = game.getHand().getPot();
        }
    }
    
    
    public void stripAllCards(){
        for (int i=0; i<players.length; i++){
            players[i].setHoleCards(null);
        }
    }
    
    public void stripCardsForPlayer(int playerSeat){
        for (int i=0; i<players.length; i++){
            if (i!=playerSeat){
                players[i].setHoleCards(null);
            }
        }
    }
    
    public Player[] getPlayers() {
        return players;
    }
    
    public int getDealerPosition() {
        return dealerPosition;
    }
    
    public BlindLevel getBlinds() {
        return blinds;
    }
    
    public Card[] getCommunityCards(){
        return this.communityCards;
    }
    
    public int getId() {
        return id;
    }
    
    public int getPot(){
        return this.pot;
    }
    
    public String getName() {
        return name;
    }
    
    public UpdateType getType(){
        return this.type;
    }
    
    public PokerBreed getBreed(){
        return this.breed;
    }
    
    public int getPlayerTurn(){
        return this.playerTurn;
    }
    
}
Before it is sent, it undergoes a deep copy: (not sure if its relevant or not)
Code:
  private TableUpdate deepCopy(TableUpdate update){
        try {
            
            // converts the object to a byte array
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(update);
            oos.close();
            byte[] byteArr = baos.toByteArray();
            
            // converts the byte array back to an object
            ByteArrayInputStream bais = new ByteArrayInputStream(byteArr);
            ObjectInputStream ois = new ObjectInputStream(bais);
            update = (TableUpdate) ois.readObject();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return update;
    }
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-11-2009, 02:46 AM
Senior Member
 
Join Date: Nov 2008
Posts: 275
Rep Power: 2
neilcoffey is on a distinguished road
Default
Essentially, the problem is that the client is getting to a point where it "gets the wrong bit of data" to what it is expecting. Without seeing all of your code, it's difficult to say more, but possible causes include:

- when writing the data, you're sending a buffer at a time, but incorrectly missing cases where not all of the buffer is written to the stream
- possibly a synchronization error if two threads are either reading or writing to the same stream for some reason
- some other logic in what object/data you're deciding to read or write to the stream when
__________________
Neil Coffey
Javamex - Java tutorials and performance info
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-18-2009, 04:07 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
Default
What Neil said is correct. To put it in more simple words, the client is expecting very specific input from the socket, where the input represents objects. If the client expects a certain object, and it gets *anything* that does not represent that object, the you get an exception.

Neil listed several possible reasons for this.

Looking at your code, I notice that you did not include the sending or the receiving code. Therefore, it's impossible to say what might be causing the problem.

My guess is that the sender and the receiver are not in complete agreement about what is being sent. Once in a while something is added or left out, which results in the error.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi Client Server applications, troubles with File I/O zackpudil Networking 3 01-29-2010 07:53 AM
I got a problem with StreamCorruptedException, any ideas? Goodwine New To Java 6 04-20-2009 05:02 PM
What does casting mean? sev51 New To Java 3 01-27-2009 05:31 PM
casting help soc86 New To Java 4 01-14-2009 12:07 AM
java.io.StreamCorruptedException elizabeth Advanced Java 1 08-06-2007 07:45 PM


All times are GMT +2. The time now is 09:25 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org