Results 1 to 2 of 2

Thread: EOFException

  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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)
    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);
    		}
    	}
    }
    The method receiveObject(and sendObject) actually works, I have tested them in a test class. But in this program, it cause EOFException. Why?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: EOFException

    For starters make your sender flush its output stream.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 11-05-2011, 09:39 AM
  2. EOFException error in I/O. Are threads the problem?
    By JavaVox in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-24-2011, 07:53 PM
  3. java.io.EOFException using readObject
    By yotamoo in forum Advanced Java
    Replies: 0
    Last Post: 12-31-2010, 10:00 AM
  4. Object Input Stream EOFException
    By FlyNn in forum New To Java
    Replies: 1
    Last Post: 12-18-2010, 12:33 PM
  5. EOFException
    By coyne20 in forum Introductions
    Replies: 0
    Last Post: 02-19-2009, 02:02 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
  •