Click to See Complete Forum and Search --> : Why EOFException occurs?


Gandurash
12-01-2007, 08:42 PM
Hello,

I am trying to send byte array from C++ to Java app, but EOFException always occurs. Why is that?

I send through this C++ code (thanks to hendrixj):
...
// Change-back endian to network order.
ni = ::htonl(hi); // "hi" index of hdata[hi][hj]
nj = ::htonl(hj); // "hj" index of hdata[hi][hj]

for(int i = 0; i < hi; i++) {
for(int j = 0; j < hj; j++) {
ndata[i][j] = ::htonl(hdata[i][j]); // every content of hdata[hi][hj]
}
}

// Sending-back the indexes and the array.
if(send(connectSocket, ∋, sizeof(int), 0) < 0) { // send-back "ni" index
cerr << "Couldn't send-back first index.";
exit(1);
}

if(send(connectSocket, &nj, sizeof(int), 0) < 0) { // send-back "nj" index
cerr << "Couldn't send-back second index.";
exit(1);
}

if(send(connectSocket, &ndata, sizeof(int) * hi * hj, 0) < 0) { // send-back ndata[hi][hj] array
cerr << "Couldn't send-back " << hi << " x " << hj << " data.";
exit(1);
}
...

And I receive using this Java code:
// Receive the byte array.
bytesNum = is.read(b);
inBytes = new byte[bytesNum];
System.arraycopy(b, 0, inBytes, 0, bytesNum);

// Read the byte stream into integer array.
bais = new ByteArrayInputStream(inBytes);
dis = new DataInputStream(bais);

ii = dis.readInt();
jj = dis.readInt();

System.out.println("i = " + ii + " j = " + jj);
newTwoD = new int[ii][jj];

System.out.print("Received: |");
for (int i = 0; i < ii; i++) {
for (int j = 0; j < jj; j++) {
newTwoD[i][j] = dis.readInt();
if (newTwoD[i][j] != arrTwoD[i][j]) {
System.err.println("newTwoD[" + i + "][" + j + "] = " + newTwoD[i][j] + " != arrTwoD[" + i + "][" + j + "] = " + arrTwoD[i][j]);
} else {
System.out.print(newTwoD[i][j] + "|");
}
}
}
System.out.println();

Is there anybody can tell me why I get this message?:confused:
java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:375)
at EchoJava.main(EchoJava.java:83)

Please help me and Thank you very much.

nspils
12-02-2007, 08:47 AM
What is the statement at line 375?

It looks to me that the program is executing a "nextInt" method and finds nothing there (it has gone beyond the end of the file). Has it read anything of the file when the exception is thrown? Is your loop detecting the end of the input stream?