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:
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
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
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.