Results 1 to 2 of 2
Thread: EOFException
- 03-01-2012, 09:58 PM #1
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)
Java 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)
The method receiveObject(and sendObject) actually works, I have tested them in a test class. But in this program, it cause EOFException. Why?Java 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); } } }
- 03-02-2012, 10:18 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
java.io.EOFException when sending object through socket
By sanderstad in forum NetworkingReplies: 1Last Post: 11-05-2011, 09:39 AM -
EOFException error in I/O. Are threads the problem?
By JavaVox in forum Threads and SynchronizationReplies: 5Last Post: 06-24-2011, 07:53 PM -
java.io.EOFException using readObject
By yotamoo in forum Advanced JavaReplies: 0Last Post: 12-31-2010, 10:00 AM -
Object Input Stream EOFException
By FlyNn in forum New To JavaReplies: 1Last Post: 12-18-2010, 12:33 PM -
EOFException
By coyne20 in forum IntroductionsReplies: 0Last Post: 02-19-2009, 02:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks