Results 1 to 9 of 9
  1. #1
    goto is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default java.io.StreamCorruptedException

    Hello,

    I try to create a game in java and I want to transmit the world of the game as a whole from the server to the client. The next code is only a reduced version of my program and I changed the complex region object of map to a normal String.

    I can not send big ammounts of objects throug the stream. Perhaps its because of the 64 Bit OS?
    Small numbers like 50 or 100 work, but big numbers like 50000 don't work.

    Its interesting, but this error never occours on my friends computer. He can even increase the number up to 10k or 100k.

    To start the program just start the server first and after that start the client.

    Server:
    Java Code:
    package problem;
    
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    
    public class ProblemServer {
    	private final int port = 90;
    	
    	public void run() {
    		ServerSocket serverSocket = null;
    		
        	try {
                serverSocket = new ServerSocket(port);
            } catch (IOException e) {
            	e.printStackTrace();
            	return;
            }
            
            Socket socket = null;
        	try {
        		System.out.println("Server listens on port "+port+".");
        		//hier warten auf die verbindung zum problemlösungsclient 
        		socket = serverSocket.accept();
        		//warten auf nachricht des problemlösungsclient
        		System.out.println("Connection to client: "+socket.getInetAddress()+" established.");
        	} catch (IOException e) {
        		e.printStackTrace();
        		return;
        	}
        	//I/O Verbindungen zum Client aufbauen
    
    		ObjectInputStream in;
    		try {
                in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
    		} catch (IOException ioe) {
    			ioe.printStackTrace();
    			return;
    		}
    	    
    	    //lese Nachricht vom Client
    	    Object vomClient=null;
    	    
    	    try {
    	    	vomClient = in.readObject();
    	    	System.out.println(vomClient);
    	    } catch (ClassNotFoundException cnfe) {
    	    	cnfe.printStackTrace();
    	    	return;
    	    } catch (IOException ioe) {
    	    	ioe.printStackTrace();
    	     	return;
    	    }
    	    
    	    try {
    		    in.close();
    		    socket.close();
    	    } catch(IOException e) {
    	    	e.printStackTrace();
    	    }
            
            try {
            	serverSocket.close();
            } catch (IOException sc) {
            	sc.printStackTrace();
            }
    	}
    	
    	
    	public static void main (String[] args) {
    		new ProblemServer().run();
    	}
    }
    Client
    Java Code:
    package problem;
    
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    
    
    public class ProblemClient {
    	private final int port = 90;
    	private final String host = "localhost";
    	
    	
    	public ProblemClient() {	
    		Socket clientSocket = null;
            ObjectOutputStream out = null;
    
            try {
                clientSocket = new Socket(host, port);
                out = new ObjectOutputStream(clientSocket.getOutputStream());
            } catch (UnknownHostException e) {
            	e.printStackTrace();
            	return;
            } catch (IOException e) {
            	e.printStackTrace();
            	return;
            }
            
            //senden der Nachricht
            try {
            	//generate map erzeugen
        		ArrayList<String> map = new ArrayList<String>();
        		
        		//parzelle
        		Region p = new Region(1,1,1);
        		for (int i = 0;i<50000;i++) {
        			//Point point = new Point(1,i);
        			//p.setZelle(new Cell(point,i), point);
        			map.add(new String(Integer.toString(i)));
        		}
        		
        		//map.add( p);
        		//daten senden
            	out.writeObject(map);
            	out.flush();
            } catch (IOException e) {
            	e.printStackTrace();
            }
            
            try {
    	    	out.close();
    		    clientSocket.close();
    		    System.out.println("Connection to client: "+clientSocket.getInetAddress()+" closed.");
    	    } catch(IOException e) {
    	    	e.printStackTrace();
    	    }
    	}
    	
    	public static void main(String[] args) {
    		new ProblemClient();
    	}
    }
    If you start it more than one time the program has different results in execution:
    - java.io.StreamCorruptedException: invalid handle value: 007E003D
    - java.io.StreamCorruptedException: invalid handle value: 007E003B
    - java.io.StreamCorruptedException: invalid type code: 00
    - java.io.StreamCorruptedException: invalid type code: 01
    - java.io.StreamCorruptedException: invalid type code: 03
    - java.io.StreamCorruptedException: invalid type code: 0E
    - java.io.StreamCorruptedException: invalid type code: 32
    - java.io.StreamCorruptedException: invalid type code: 3F
    - java.io.StreamCorruptedException: unexpected end of block data

    But also:
    - Exception in thread "main" java.lang.NullPointerException
    - Exception in thread "main" java.lang.ClassCastException: java.awt.Point cannot be cast to java.io.ObjectStreamClass
    - java.io.OptionalDataException
    - java.io.EOFException

    The errors are always in Line number 48 of the problemServer vomClient = in.readObject();

    The ProblemClient throws:
    - java.net.SocketException: Connection reset by peer: socket write error
    ...

    If I use only 500 Strings it doesn't throw the exception on the client but for 50000 it is thrown.

    The results seem to be totally randomly. If you start the configuration more than one time in short periods, the errors seem to change every time.

    --------------------------------------------------------------------------

    I use:
    Windows 7 Home Premium (64 Bit)

    Java(TM) 6 Update 23 (Version 6.0.230)

    Eclipse Platform

    Version: 3.6.1.r361_v20100909-9gF78GrkFqw7GrsZnvz0JWNTeb6fue6896L
    Build id: M20100909-0800
    Last edited by goto; 01-02-2011 at 09:51 AM. Reason: Make the example easier

  2. #2
    goto is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    This problem could be the same but for file IO:
    Java Forums » Java SE » Networking » JavaTrouble resolving a StreamCorruptedException

  3. #3
    goto is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default Compatibility Problems with win7 64 Bit

    I used a VM with winxp 32 bit on the same computer an now it works, also with 500000 and more Strings. But on win 7 64 bit it doesn't work. Or perhaps only on my computer ...
    Please report, if this code doesn't work on other 64 bit os.

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Usually, these type of client server issues are related to failure to flush on object boundaries. On your client, instead of trying to serialize directly into the socket, serialize into a byte array instead, and then write the byte array to the socket, ensuring a flush after each write.

  5. #5
    goto is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the advice. I tried to follow it, but its causing a new problem on both winxp 32 and win7 64:

    I use this funktions to convert:
    Java Code:
           public static byte[] toBytes(Object object){
    		java.io.ByteArrayOutputStream baos = new
    		java.io.ByteArrayOutputStream();
    		try{
    			java.io.ObjectOutputStream oos = new
    			java.io.ObjectOutputStream(baos);
    			oos.writeObject(object);
    		}catch(java.io.IOException ioe){
    			ioe.printStackTrace();
    		}
    		
    		return baos.toByteArray();
    	} 
    	
    	public static Object toObject(byte[] bytes){
    		Object object = null;
    		try{
    			object = new java.io.ObjectInputStream(new
    			java.io.ByteArrayInputStream(bytes)).readObject();
    		}catch(java.io.IOException ioe){
    			ioe.printStackTrace();
    		}catch(java.lang.ClassNotFoundException cnfe){
    			cnfe.printStackTrace();
    		}
    		return object;
    	}
    I changed the ObjectIn- and OutputStreams to just In- and OutputStreams That means:
    out = clientSocket.getOutputStream (in the client)
    in = new BufferedInputStream(socket.getInputStream) (in the server)

    The Client instead of just out.writeObject(map);:
    Java Code:
    byte[] a = toBytes(map);
    Object o = toObject(a); //works fine	
    //daten senden
    out.write(a);
    out.flush();
    The Server instead of just in.readObject();:
    Java Code:
    int n = in.available();
    byte[] b = new byte[n];
    in.read(b);
    now it Throws an :
    java.net.SocketException: Connection reset by peer: socket write error
    java.net.SocketException: Software caused connection abort: socket write error

    An Object o = toObject(b); on the server causes an EOFException in this line:
    object = new java.io.ObjectInputStream(new
    java.io.ByteArrayInputStream(bytes)).readObject();

    Of toObject(Byte[] b)

    If you need more codeexamples just ask.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    There is no guarantee that "in.available()" is going to return exactly the number of bytes in a serialized object - in fact it almost never will.

    So you'll need to add some kind of header to each packet of bytes you send across to tell the server how many bytes to expect. The header needs to be small (less than the underlying socket buffer size, which is usually 65k by default). The simplest header of course could just be an integer that is the number of bytes in the serialized object that follows. However, if you think your game might end up being distributed, then it'd make sense to have the header also include a version ID, so that later on you can make servers that work with older clients.

    Also, when you read the data in, you need to ensure it is fully read. "in.read(byte[])" generally does not guarantee that the amount read is the same as the size of the byte array. If you wrap the InputStream with a DataInputStream, then you can use "DataInputStream.readFully()" to ensure that every byte is read. This is also the mechanism you can use to deserialize the object size prior to deserializing the object...

    client pseudocode
    Java Code:
      DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
     
      dos.writeInt(bytes.length);
      dos.write(bytes);
    server psuedocode
    Java Code:
      DataInputStream din = new DataInputStream(socket.getInputStream());
    
      int size = din.readInt();
    
      byte[] bytes = new byte[size];
    
      din.readFully(bytes);
    ...then you can deserialize the Object contained in 'bytes' confident it is nothing more nor less than a complete Object.

  7. #7
    goto is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank you very much. It works fine on both os.

  8. #8
    pradeep is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default Re: java.io.StreamCorruptedException

    could you please send us the entire code, problme you face, since we are also facing the same issue.
    2013-11-14 17:51:18,526 WARN Thread-3 commh.md.client.util.ErrorHandler - java.rmi.UnmarshalException: failed to unmarshal class [Lcom.mh.md.common.entity.business.poBusiness.Purch aseOrderImmutable;; nested exception is:
    java.io.StreamCorruptedException: invalid type code: 00java.io.StreamCorruptedException: invalid type code: 00

    Please let us know what will be the issue, since we migrated code from 1.5 to 16 when we run the appliation we are facing this issue.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: java.io.StreamCorruptedException

    Dude pay attention; the thread is 2 years old. Plus nobody in their right mind is going to send you code that is likely owned by their boss; that's a lawsuit waiting to happen.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. StreamCorruptedException
    By wikisb in forum New To Java
    Replies: 3
    Last Post: 10-28-2010, 07:16 AM
  2. Replies: 0
    Last Post: 05-17-2010, 02:45 PM
  3. StreamCorruptedException
    By cristo_haris in forum Advanced Java
    Replies: 11
    Last Post: 04-20-2009, 03:44 PM
  4. StreamCorruptedException and Casting troubles
    By Wassa in forum Networking
    Replies: 2
    Last Post: 02-18-2009, 03:07 PM
  5. java.io.StreamCorruptedException
    By elizabeth in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 06:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •