-
EOFException
I wanted to create a simple method that send a Serializable object/Object through a socket. But it wasnt as easy as I thought, I am getting exception and I dont know why.
The receiver (client)
Code:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2298)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2767)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:798)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at Client.receiveObject(Client.java:101)
at Client.receiveFile(Client.java:60)
at Client.receiveFile(Client.java:93)
at Client.main(Client.java:26)
Exception in thread "main" java.lang.NullPointerException
at Client.receiveFile(Client.java:60)
at Client.receiveFile(Client.java:93)
at Client.main(Client.java:26)
Code:
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
public class Client
{
private static final int BUFFER_SIZE = 65536;
static File[] files =
{new File ("E:\\test1.txt"),
new File ("E:\\test2.txt"),
new File ("E:\\test3.txt")};
public static void main(String[] args) throws Exception
{
enableDebugging ();//Ignore this
System.out.println ("Client");
Socket sock = null;
try
{
sock =new Socket ("yrm0dder.no-ip.org", 40000);
System.out.println ("Connected");
receiveFile(sock, files);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try {
sock.close ();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void receiveFile (Socket sock, File destination)
{
BufferedOutputStream fileOut = null;
try
{
//Receive data from socket
InputStream netIn = sock.getInputStream();
//Write bytes to a file
fileOut = new BufferedOutputStream (new FileOutputStream (destination));
byte[] buffer = new byte[BUFFER_SIZE];
int readBytes;
//Receive the file size
long fileSize = (Long) receiveObject (sock);
long counter = 0;
do
{
readBytes = netIn.read (buffer);
fileOut.write (buffer, 0, readBytes);
}
while ((counter += readBytes) < fileSize);
}
catch (IOException e)
{
e.printStackTrace ();
}
finally
{
if (fileOut != null)
{
try
{
fileOut.close ();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
//Receive files via socket
public static void receiveFile (Socket sock, File[] destination)
{
for (int i = 0; i < destination.length; i++)
receiveFile (sock, destination[i]);
}
public static Serializable receiveObject (final Socket sock)
{
Serializable obj = null;
try
{
ObjectInputStream objIn = new ObjectInputStream (sock.getInputStream());
obj = (Serializable) objIn.readObject();
}
catch (IOException | ClassNotFoundException e)
{
e.printStackTrace ();
}
return obj;
}
public static void enableDebugging () //Never mind what this does
{
Date date = new Date ();
DateFormat df = DateFormat.getDateInstance();
String theDate = df.format(date);
java.io.File file = new java.io.File ("debug " + theDate + ".log");
try
{
System.setErr (new java.io.PrintStream (new java.io.FileOutputStream (file)));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
System.exit (-1);
}
}
}
The method receiveObject(and sendObject) actually works, I have tested them in a test class. But in this program, it cause EOFException. Why?
-
Re: EOFException
For starters make your sender flush its output stream.
kind regards,
Jos