Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: EOFException

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default EOFException

    Hello all,

    I'm trying to make my own dropbox kind of program. Just for the funs of it, and extreme customisation.
    Now i've got a small problem while trying to send the file info in advance. Mostly the first time i connect everything goes just fine.
    But as soon as another client connects it crashes, so i guess it's something with my server that is wrong. i get the following error:
    java.io.EOFException
    	at java.io.DataInputStream.readInt(Unknown Source)
    	at FileInfo.read(FileInfo.java:38)
    	at FileSendRecieve.recieveFile(FileSendRecieve.java:30)
    	at Client.run(Client.java:24)
    	at java.lang.Thread.run(Unknown Source)
    I've been googling around this error, but find it quite hard to find a solution or anything for my problem. The only solution there seems to be is closing the sockets after usage. But this i already do. I found out that this error occurs when i try to write something to a closed socket.
    This class is used to store the information and then send it to the target
    	import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.IOException;
     
    public class FileInfo {
    	private String name;
    	private String dir;
    	private long size;
    	private String checkSum = null;
     
    	public FileInfo() {
     
    	}
     
    	public FileInfo(String name, String dir, long size) {
    		this.name = name;
    		this.dir = dir;
    		this.size = size;
    	}
     
    	public FileInfo(File file) {
    		this.name = file.getName();
    		this.dir = file.getPath();
    		this.size = file.length();
    	}
     
    	public void setChecksum(String checkSum) {
    		this.checkSum = checkSum;
    	}
     
    	/*
    	 * Protocol: first int: Amount of data is going to be send second int: type
    	 * of data third variable: data
    	 */
    	public void read(DataInputStream dis) throws IOException {
    		int dataAmount = 0;
    		dataAmount = dis.readInt();
    		for (int i = 0; i < dataAmount; i++) {
    			int choice = 0;
    			choice = dis.readInt();
    			switch (choice) {
    			case 0:
    				// do nothing
    				break;
    			case 1:
    				name = readString(dis);
    				break;
    			case 2:
    				dir = readString(dis);
    				break;
    			case 3:
    				size = dis.readLong();
    				break;
    			}
    		}
    	}
     
    	/*
    	 * test
    	 * another test
    	 */
    	public void write(DataOutputStream dos) throws IOException {
    		int dataAmount = 3;
    		if (checkSum != null)
    			dataAmount++;
    		dos.writeInt(dataAmount);
    		dos.writeInt(1);
    		writeString(dos, name);
    		dos.writeInt(2);
    		writeString(dos, dir);
    		dos.writeInt(3);
    		dos.writeLong(size);
    		dos.flush();
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public String getDir() {
    		return dir;
    	}
     
    	public long getSize() {
    		return size;
    	}
     
    	public boolean check(String checkSum) {
    		if (this.checkSum.equals(checkSum))
    			return true;
    		return false;
    	}
     
    	/**
    	 * Read string.
    	 * 
    	 * @param dis
    	 *            the dis
    	 * @return the string
    	 * @throws IOException
    	 *             Signals that an I/O exception has occurred.
    	 */
    	protected String readString(DataInputStream dis) throws IOException {
    		int length = dis.readInt();
    		StringBuilder sb = new StringBuilder(length);
     
    		for (int i = 0; i < length; i++) {
    			sb.append(dis.readChar());
    		}
    		return sb.toString();
    	}
     
    	/**
    	 * Write string.
    	 * 
    	 * @param dos
    	 *            the dos
    	 * @param string
    	 *            the string
    	 * @throws IOException
    	 *             Signals that an I/O exception has occurred.
    	 */
    	protected void writeString(DataOutputStream dos, String string)
    			throws IOException {
    		int length = string.length();
    		char[] data = string.toCharArray();
     
    		dos.writeInt(length);
    		for (int i = 0; i < length; i++) {
    			dos.writeChar(data[i]);
    		}
    	}
     
    	public void print() {
    		System.out.println("Name:" + name + " Size:" + size + " dir:" + dir);
    	}
    }

    A commonly class to send and recieve file info:
    public class FileSendRecieve {
    	Socket socket;
    	OutputStream socketOut;
    	InputStream socketIn;
     
    	public FileSendRecieve(Socket socket) {
    		try {
    			socketOut = socket.getOutputStream();
    			socketIn = socket.getInputStream();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		this.socket = socket;
    	}
     
    	public void sendFile(String name, String directory) throws IOException {
    		File file = new File(name);
    		FileInfo fileinf = new FileInfo(file);
    		fileinf.write(SocketUtil.wrapOutputStream(socket));
    	}
     
    	public void recieveFile() throws IOException {
    		FileInfo fileinf = new FileInfo();
    		fileinf.read(SocketUtil.wrapInputStream(socket));
    		fileinf.print();
    	}
     
    	public void stop() {
    		SocketUtil.close(socketIn);
    		SocketUtil.close(socketOut);
    		SocketUtil.close(socket);
    	}
    }

    My server:

    public class Server extends Thread {
     
    	private boolean run = false;
    	private float runTime;
    	private ServerSocket ss = null;
    	private Socket cs = null;
     
    	public Server() {
    		runTime = System.nanoTime();
    	}
     
    	public void startService() {
    		try {
    			ss = new ServerSocket(8081);
    			run = true;
    		} catch (IOException e) {
    			System.exit(0);
    		}
    		this.start();
    	}
     
    	public void run() {
    		while (run) {
    			try {
    				cs = ss.accept();
    				new Thread(new ClientHandler(cs)).start();
    			} catch (Exception e) {
    				System.exit(1);
    			}
    		}
    	}
     
    	class ClientHandler implements Runnable {
    		FileSendRecieve fsr;
     
    		public ClientHandler(Socket cs) {
    			fsr = new FileSendRecieve(cs);
    		}
     
    		public void run() {
    			try {
    				fsr.sendFile("image.png", "c:\\");
    				fsr.sendFile("image2.png", "c:\\");
    				fsr.stop();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
     
    	public static void main(String args[]) {
    		Server server = new Server();
    		server.startService();
    	}
    }

    and my client

    import java.io.IOException;
    import java.net.Socket;
     
    public class Client implements Runnable {
    	FileSendRecieve fsr;
    	public Client(Socket socket) {
    		fsr = new FileSendRecieve(socket);
    	}
     
    	public static void main(String args[]) {
    		Socket s = null;
    		try {
    			s = new Socket("127.0.0.1", 8081);
    		} catch (Exception e) {
    			System.exit(0);
    		}
    		new Thread(new Client(s)).start();
    	}
     
    	@Override
    	public void run() {
    		try {
    			fsr.recieveFile();
    			fsr.recieveFile();
    			fsr.stop();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

    Also here is my socketWrapper. i use it to wrap my sockets in. And i use the close functions.
    /**
     * This class contains some useful socket and networking utilities.
     * 
     * @author Elmar
     */
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.Closeable;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.DatagramSocket;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    public class SocketUtil {
     
    	/**
    	 * This class wraps a output socket into a buffered data outputStream
    	 * 
    	 * @param socket
    	 *            The socked to wrap
    	 * @return Returns the Buffered dataOutputStream
    	 * @throws IOException
    	 *             Should get handled in the function itself
    	 */
    	public static DataOutputStream wrapOutputStream(Socket socket)
    			throws IOException {
    		return new DataOutputStream(new BufferedOutputStream(
    				socket.getOutputStream()));
    	}
     
    	/**
    	 * This class wraps a output socket into a buffered data inputStream
    	 * 
    	 * @param socket
    	 *            The socked to wrap
    	 * @return Buffered dataInputStream
    	 * @throws IOException
    	 *             Should get handled in the function itself
    	 */
    	public static DataInputStream wrapInputStream(Socket socket)
    			throws IOException {
    		return new DataInputStream(new BufferedInputStream(
    				socket.getInputStream()));
    	}
     
    	/**
    	 * Closes the given {@link Closeable}.
    	 * 
    	 * @param closeable
    	 *            the closeable
    	 */
    	public static void close(Closeable closeable) {
    		try {
    			if (closeable != null) {
    				closeable.close();
    			}
    		} catch (IOException exception) {
    		}
    	}
     
    	/**
    	 * Closes the given {@link Socket}.
    	 * 
    	 * @param socket
    	 *            the socket
    	 */
    	public static void close(Socket socket) {
    		try {
    			if (socket != null) {
    				socket.close();
    			}
    		} catch (IOException exception) {
    		}
    	}
     
    	/**
    	 * Closes the given {@link ServerSocket}.
    	 * 
    	 * @param serverSocket
    	 *            the server socket
    	 */
    	public static void close(ServerSocket serverSocket) {
    		try {
    			if (serverSocket != null) {
    				serverSocket.close();
    			}
    		} catch (IOException exception) {
    		}
    	}
     
    	/**
    	 * Closes the given {@link DatagramSocket}.
    	 * 
    	 * @param socket
    	 *            the socket
    	 */
    	public static void close(DatagramSocket socket) {
    		if (socket != null) {
    			socket.close();
    		}
    	}
    }
    Last edited by elamre; July 27th, 2012 at 07:57 AM. Reason: Added code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    error occurs when i try to write something to a closed socket.
    Can you change your logic so it does not try writing to a closed socket?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    I wish i could, but according to my logic it should NOT be doing that. That's where it goes wrong.
    It's just a conclusion i made after doing research on the internet.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    If you want help with your code you will need to post a small complete program that compiles, executes and shows the problem.

    This looks like code that will loop forever:
    	while(true);
    Why is it in the program?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    ok I'll update my post with the complete code.

    The
    while(true)
    is there because i didnt want to close the socket. Just a test

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    Ok i updated my main post. Its a lot of code to process though. But i'd really like to fix it!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    I see some of the code does not have import statements.
    Can you put the code into a single java file that can be compiled and executed to show the problem?
    Make a driver class with a main method that calls each of the main methods on their own threads.
    Something like this:
       public static void main(final String[] args) { //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
          Thread t1 = new Thread(new Runnable() {
             public void run() {
                Server.main(args);
             }
          });
          t1.start();
          Thread t2 = new Thread(new Runnable() {
             public void run() {
                Client.main(args);
             }
          });
          t2.start();
     
          // wait some
          try{Thread.sleep(5000);}catch(Exception x){}
          System.exit(0); // done
     
       }
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    Nvm, misunderstood it wrongly. Doing it right now.
    Last edited by elamre; July 27th, 2012 at 08:19 AM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    You may have to make the inner classes static for it to compile.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    Hmm, im not very good at this I cant even seem to run it.
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.Closeable;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.DatagramSocket;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    public class AllInOne {
     
    	public static class Server extends Thread {
     
    		private boolean run = false;
    		private ServerSocket ss = null;
    		private Socket cs = null;
     
    		public Server() {
    		}
     
    		public void startService() {
    			try {
    				ss = new ServerSocket(8081);
    				run = true;
    			} catch (IOException e) {
    				System.exit(0);
    			}
    			this.start();
    		}
     
    		public void run() {
    			while (run) {
    				try {
    					cs = ss.accept();
    					new Thread(new ClientHandler(cs)).start();
    				} catch (Exception e) {
    					System.exit(1);
    				}
    			}
    		}
     
    		class ClientHandler implements Runnable {
    			FileSendRecieve fsr;
     
    			public ClientHandler(Socket cs) {
    				AllInOne aio = new AllInOne();
    				fsr = aio.new FileSendRecieve(cs);
    			}
     
    			public void run() {
    				try {
    					fsr.sendFile("image.png", "c:\\");
    					fsr.sendFile("image2.png", "c:\\");
    					fsr.stop();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
     
    		public static void main(String args[]) {
    			Server server = new Server();
    			server.startService();
    		}
    	}
     
    	public static class Client implements Runnable {
    		FileSendRecieve fsr;
    		public Client(Socket socket) {
    			AllInOne aio = new AllInOne();
    			fsr = aio.new FileSendRecieve(socket);
    		}
     
    		public static void main(String args[]) {
    			Socket s = null;
    			try {
    				s = new Socket("127.0.0.1", 8081);
    			} catch (Exception e) {
    				System.exit(0);
    			}
    			new Thread(new Client(s)).start();
    		}
     
    		@Override
    		public void run() {
    			try {
    				fsr.recieveFile();
    				fsr.recieveFile();
    				fsr.stop();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
     
    	public class FileInfo {
    		private String name;
    		private String dir;
    		private long size;
    		private String checkSum = null;
     
    		public FileInfo() {
     
    		}
     
    		public FileInfo(String name, String dir, long size) {
    			this.name = name;
    			this.dir = dir;
    			this.size = size;
    		}
     
    		public FileInfo(File file) {
    			this.name = file.getName();
    			this.dir = file.getPath();
    			this.size = file.length();
    		}
     
    		public void setChecksum(String checkSum) {
    			this.checkSum = checkSum;
    		}
     
    		/*
    		 * Protocol: first int: Amount of data is going to be send second int: type
    		 * of data third variable: data
    		 */
    		public void read(DataInputStream dis) throws IOException {
    			int dataAmount = 0;
    			dataAmount = dis.readInt();
    			for (int i = 0; i < dataAmount; i++) {
    				int choice = 0;
    				choice = dis.readInt();
    				switch (choice) {
    				case 0:
    					// do nothing
    					break;
    				case 1:
    					name = readString(dis);
    					break;
    				case 2:
    					dir = readString(dis);
    					break;
    				case 3:
    					size = dis.readLong();
    					break;
    				}
    			}
    		}
     
    		/*
    		 * test
    		 * another test
    		 */
    		public void write(DataOutputStream dos) throws IOException {
    			int dataAmount = 3;
    			if (checkSum != null)
    				dataAmount++;
    			dos.writeInt(dataAmount);
    			dos.writeInt(1);
    			writeString(dos, name);
    			dos.writeInt(2);
    			writeString(dos, dir);
    			dos.writeInt(3);
    			dos.writeLong(size);
    			dos.flush();
    		}
     
    		public String getName() {
    			return name;
    		}
     
    		public String getDir() {
    			return dir;
    		}
     
    		public long getSize() {
    			return size;
    		}
     
    		public boolean check(String checkSum) {
    			if (this.checkSum.equals(checkSum))
    				return true;
    			return false;
    		}
     
    		/**
    		 * Read string.
    		 * 
    		 * @param dis
    		 *            the dis
    		 * @return the string
    		 * @throws IOException
    		 *             Signals that an I/O exception has occurred.
    		 */
    		protected String readString(DataInputStream dis) throws IOException {
    			int length = dis.readInt();
    			StringBuilder sb = new StringBuilder(length);
     
    			for (int i = 0; i < length; i++) {
    				sb.append(dis.readChar());
    			}
    			return sb.toString();
    		}
     
    		/**
    		 * Write string.
    		 * 
    		 * @param dos
    		 *            the dos
    		 * @param string
    		 *            the string
    		 * @throws IOException
    		 *             Signals that an I/O exception has occurred.
    		 */
    		protected void writeString(DataOutputStream dos, String string)
    				throws IOException {
    			int length = string.length();
    			char[] data = string.toCharArray();
     
    			dos.writeInt(length);
    			for (int i = 0; i < length; i++) {
    				dos.writeChar(data[i]);
    			}
    		}
     
    		public void print() {
    			System.out.println("Name:" + name + " Size:" + size + " dir:" + dir);
    		}
    	}
     
    	public class FileSendRecieve {
    		Socket socket;
    		OutputStream socketOut;
    		InputStream socketIn;
     
    		public FileSendRecieve(Socket socket) {
    			try {
    				socketOut = socket.getOutputStream();
    				socketIn = socket.getInputStream();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			this.socket = socket;
    		}
     
    		public void sendFile(String name, String directory) throws IOException {
    			File file = new File(name);
    			FileInfo fileinf = new FileInfo(file);
    			fileinf.write(SocketUtil.wrapOutputStream(socket));
    		}
     
    		public void recieveFile() throws IOException {
    			FileInfo fileinf = new FileInfo();
    			fileinf.read(SocketUtil.wrapInputStream(socket));
    			fileinf.print();
    		}
     
    		public void stop() {
    			SocketUtil.close(socketIn);
    			SocketUtil.close(socketOut);
    			SocketUtil.close(socket);
    		}
    	}
     
    	public static class SocketUtil {
     
    		/**
    		 * This class wraps a output socket into a buffered data outputStream
    		 * 
    		 * @param socket
    		 *            The socked to wrap
    		 * @return Returns the Buffered dataOutputStream
    		 * @throws IOException
    		 *             Should get handled in the function itself
    		 */
    		public static DataOutputStream wrapOutputStream(Socket socket)
    				throws IOException {
    			return new DataOutputStream(new BufferedOutputStream(
    					socket.getOutputStream()));
    		}
     
    		/**
    		 * This class wraps a output socket into a buffered data inputStream
    		 * 
    		 * @param socket
    		 *            The socked to wrap
    		 * @return Buffered dataInputStream
    		 * @throws IOException
    		 *             Should get handled in the function itself
    		 */
    		public static DataInputStream wrapInputStream(Socket socket)
    				throws IOException {
    			return new DataInputStream(new BufferedInputStream(
    					socket.getInputStream()));
    		}
     
    		/**
    		 * Closes the given {@link Closeable}.
    		 * 
    		 * @param closeable
    		 *            the closeable
    		 */
    		public static void close(Closeable closeable) {
    			try {
    				if (closeable != null) {
    					closeable.close();
    				}
    			} catch (IOException exception) {
    			}
    		}
     
    		/**
    		 * Closes the given {@link Socket}.
    		 * 
    		 * @param socket
    		 *            the socket
    		 */
    		public static void close(Socket socket) {
    			try {
    				if (socket != null) {
    					socket.close();
    				}
    			} catch (IOException exception) {
    			}
    		}
     
    		/**
    		 * Closes the given {@link ServerSocket}.
    		 * 
    		 * @param serverSocket
    		 *            the server socket
    		 */
    		public static void close(ServerSocket serverSocket) {
    			try {
    				if (serverSocket != null) {
    					serverSocket.close();
    				}
    			} catch (IOException exception) {
    			}
    		}
     
    		/**
    		 * Closes the given {@link DatagramSocket}.
    		 * 
    		 * @param socket
    		 *            the socket
    		 */
    		public static void close(DatagramSocket socket) {
    			if (socket != null) {
    				socket.close();
    			}
    		}
    	}
     
    	public static void main(String args[]) {
    	      Thread t1 = new Thread(new Runnable() {
    	         public void run() {
    	            Server.main(null);
    	            System.out.println("Server started");
    	         }
    	      });
    	      t1.start();
    	      Thread t2 = new Thread(new Runnable() {
    	         public void run() {
    	            Client.main(null);
    	         }
    	      });
    	      t2.start();
     
    	      // wait some
    	      try{Thread.sleep(5000);}catch(Exception x){}
    	      System.exit(0); // done
     
    	   }
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    I cant even seem to run it.
    Please explain what happens.

    What files does the program need to execute? I see reference to some .png files.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    Silly me, i just reinstalled my pc and forgot to install JDK.

    Hmm, the odd thing is, in the AllInOne class, it works. But whenever i try to run it using the classes, it doesnt work. Would you mind looking at the project if i zipped it? Then all you need to do is import the project and run the server and the client. Because I'm still getting the error when i launch them individually.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    Interesting that the AllInOne version works. Have you tried it with more than one client?

    My IDE is very simple and does not support "projects". That's why I had you put the classes in a single file.
    One advantage I find with everything in one file is that the debugging print outs are in the order that the statements are executed.

    I notice that there is very little debugging println statements.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    I have tried it with several clients, and putting sleeps in between so it takes a while. And without sleeps as well.

    There is a lot of debugging, however i use my own made Logger for this, and found it not necessary to include this as well. And to make sure everything runs, i removed the calls to my logger.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    What could be different about the AllInOne version that it doesn't have the problem?

    Can you post the full trace/debug output for when the program gets the error?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    I dont really know what the difference could be. The only thing is that all the classes are nested now. And that everything gets executed out of 1 class.
    The output for the first client connected:
    Name:image.png Size:32888 dir:image.png
    Name:image2.png Size:67145 dir:image2.png
    And the second client:
    Name:image.png Size:32888 dir:image.png
    java.io.EOFException
    	at java.io.DataInputStream.readInt(Unknown Source)
    	at FileInfo.read(FileInfo.java:38)
    	at FileSendRecieve.recieveFile(FileSendRecieve.java:30)
    	at Client.run(Client.java:24)
    	at java.lang.Thread.run(Unknown Source)

    The server will give this as output:
    2012-07-27 19:07:13.658 - SYSTEM: Logger initalized
    2012-07-27 19:07:13.660 - SYSTEM: Server starting up. . . 
    2012-07-27 19:07:13.667 - SYSTEM: Server started succesfully in: 0.069206016 seconds.
    2012-07-27 19:07:13.667 - SYSTEM: Server is now accepting clients. . . 
    2012-07-27 19:07:13.667 - SYSTEM: Waiting for client. . . 
    2012-07-27 19:07:17.703 - SYSTEM: Client connected Socket[addr=/127.0.0.1,port=50775,localport=8081]
    2012-07-27 19:07:17.706 - SYSTEM: Waiting for client. . . 
    2012-07-27 19:07:48.775 - SYSTEM: Client connected Socket[addr=/127.0.0.1,port=50887,localport=8081]
    2012-07-27 19:07:48.776 - SYSTEM: Waiting for client. . .

    So for the server everything seems to be alright.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    Not enough debug output to see what is happening.

    Here's what I am getting:
    Running: java AllInOne

    Server started
    Client0 being started
    Client constru socket=Socket[addr=/127.0.0.1,port=8081,localport=51394]
    Server connection cs=Socket[addr=/127.0.0.1,port=51394,localport=8081]
    CH constru cs=Socket[addr=/127.0.0.1,port=51394,localport=8081]
    FSR - exiting constructor socket=Socket[addr=/127.0.0.1,port=8081,localport=51394]
    Client0 started
    FSR - exiting constructor socket=Socket[addr=/127.0.0.1,port=51394,localport=8081]
    FSR entering sendFile name=images/RedCar.png
    FI exiting write size=86
    FSR exiting sendFile name=images/RedCar.png
    FSR entering sendFile name=images/BlueCar.png
    FI exiting write size=90
    FSR exiting sendFile name=images/BlueCar.png
    closed java.net.SocketInputStream@f11de2
    closed java.net.SocketOutputStream@1e1153a
    closed socket=Socket[addr=/127.0.0.1,port=51394,localport=8081]
    CH run exiting
    0 Client run entered <<<<<<<<<<<
    FSR entering receiveFile
    FI read dis=java.io.DataInputStream@17d26fc,avail=176
    FI read dA=3, cnt=0
    FI read choice=1
    FI read name=RedCar.png
    FI read choice=2
    FI read dir=images\RedCar.png
    FI read choice=3
    FI read size=1137
    Name:RedCar.png Size:1137 dir:images\RedCar.png
    FSR exiting receiveFile
    FSR entering receiveFile
    FI read dis=java.io.DataInputStream@18a8bfa,avail=0
    Client1 being started
    Server connection cs=Socket[addr=/127.0.0.1,port=51395,localport=8081]
    CH constru cs=Socket[addr=/127.0.0.1,port=51395,localport=8081]
    FSR - exiting constructor socket=Socket[addr=/127.0.0.1,port=51395,localport=8081]
    FSR entering sendFile name=images/RedCar.png
    FI exiting write size=86
    FSR exiting sendFile name=images/RedCar.png
    FSR entering sendFile name=images/BlueCar.png
    FI exiting write size=90
    FSR exiting sendFile name=images/BlueCar.png
    closed java.net.SocketInputStream@e73236
    closed java.net.SocketOutputStream@918c34
    closed socket=Socket[addr=/127.0.0.1,port=51395,localport=8081]
    CH run exiting
    Client constru socket=Socket[addr=/127.0.0.1,port=8081,localport=51395]
    FSR - exiting constructor socket=Socket[addr=/127.0.0.1,port=8081,localport=51395]
    Client1 started
    1 Client run entered <<<<<<<<<<<
    FSR entering receiveFile
    FI read dis=java.io.DataInputStream@137ef34,avail=176
    FI read dA=3, cnt=0
    FI read choice=1
    FI read name=RedCar.png
    FI read choice=2
    FI read dir=images\RedCar.png
    FI read choice=3
    FI read size=1137
    Name:RedCar.png Size:1137 dir:images\RedCar.png
    FSR exiting receiveFile
    FSR entering receiveFile
    FI read dis=java.io.DataInputStream@139626c,avail=0
    main exiting

    0 error(s)
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    Where would you say is a good place to debug?
    I added some debug lines:
    Server output:
    2012-07-27 20:29:11.830 - SYSTEM: Logger initalized
    2012-07-27 20:29:11.831 - DEBUG: Recieving first file!
    2012-07-27 20:29:11.832 - DEBUG: Amount of data: 3
    2012-07-27 20:29:11.832 - DEBUG: Name: image.png
    2012-07-27 20:29:11.833 - DEBUG: Dir: image.png
    2012-07-27 20:29:11.833 - DEBUG: Size: 32888
    2012-07-27 20:29:11.833 - DEBUG: Name:image.png Size:32888 dir:image.png
    2012-07-27 20:29:11.833 - DEBUG: Recieving second file!
    java.io.EOFException
    	at java.io.DataInputStream.readInt(Unknown Source)
    	at FileInfo.read(FileInfo.java:38)
    	at FileSendRecieve.recieveFile(FileSendRecieve.java:30)
    	at Client.run(Client.java:26)
    	at java.lang.Thread.run(Unknown Source)
    And the output from the server:
    2012-07-27 20:29:07.548 - SYSTEM: Logger initalized
    2012-07-27 20:29:07.549 - SYSTEM: Server starting up. . . 
    2012-07-27 20:29:07.553 - SYSTEM: Server started succesfully in: 0.035651584 seconds.
    2012-07-27 20:29:07.554 - SYSTEM: Server is now accepting clients. . . 
    2012-07-27 20:29:07.554 - SYSTEM: Waiting for client. . . 
    2012-07-27 20:29:11.802 - SYSTEM: Client connected Socket[addr=/127.0.0.1,port=62686,localport=8081]
    2012-07-27 20:29:11.804 - SYSTEM: Waiting for client. . . 
    2012-07-27 20:29:11.805 - DEBUG: Sending 3 amount of data
    2012-07-27 20:29:11.805 - DEBUG: Send The following data:
    2012-07-27 20:29:11.806 - DEBUG: Name:image.png Size:32888 dir:image.png
    2012-07-27 20:29:11.806 - DEBUG: Sending 3 amount of data
    2012-07-27 20:29:11.806 - DEBUG: Send The following data:
    2012-07-27 20:29:11.806 - DEBUG: Name:image2.png Size:67145 dir:image2.png
    I added debug lines here(client):
    	public void run() {
    		try {
    			Logger.getInstance().debug("Recieving first file!");
    			fsr.recieveFile();
    			Logger.getInstance().debug("Recieving second file!");
    			fsr.recieveFile();
    			fsr.stop();
    			Logger.getInstance().system("Client stopped");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    and in the Info class:
    	public void read(DataInputStream dis) throws IOException {
    		int dataAmount = 0;
    		dataAmount = dis.readInt();
    		Logger.getInstance().debug("Amount of data: " + dataAmount);
    		for (int i = 0; i < dataAmount; i++) {
     
    			int choice = 0;
    			choice = dis.readInt();
    			switch (choice) {
    			case 0:
    				// do nothing
    				break;
    			case 1:
    				name = readString(dis);
    				Logger.getInstance().debug("Name: " + name);
    				break;
    			case 2:
    				dir = readString(dis);
    				Logger.getInstance().debug("Dir: " + dir);
    				break;
    			case 3:
    				size = dis.readLong();
    				Logger.getInstance().debug("Size: " + size);
    				break;
    			}
    		}
    	}
     
    	/*
    	 * test another test
    	 */
    	public void write(DataOutputStream dos) throws IOException {
    		int dataAmount = 3;
    		if (checkSum != null)
    			dataAmount++;
    		Logger.getInstance().debug("Sending " + dataAmount + " amount of data");
    		dos.writeInt(dataAmount);
    		dos.writeInt(1);
    		writeString(dos, name);
    		dos.writeInt(2);
    		writeString(dos, dir);
    		dos.writeInt(3);
    		dos.writeLong(size);
    		dos.flush();
    		Logger.getInstance().debug("Send The following data:");
    		print();
    	}

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    Where would you say is a good place to debug?
    Everywhere that something happens.
    The values of all the variables needs to be displayed.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    Hmm, i really have the idea that something might be wrong with the server. I could not see a reason why the client would not work? I printed out the values, and everything seems fine and should be fine. But yet i get this error .

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    Have you traced the bytes written counts and the bytes available to be read?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    I have not, and i would not exactly know how i could do that? Is there some sort of function which tells me the amount of bytes that is available to read?

    edit:
    nvm, that would be the is.avaible() function ofcourse. my bad! Let me try to find out

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: EOFException

    Read the API doc for the classes and find the methods that give that data.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    Ok i found something interesting, the second thime there are 0 bytes available to read.
    2012-07-27 21:05:00.736 - SYSTEM: Logger initalized
    2012-07-27 21:05:00.737 - DEBUG: Recieving first file!
    2012-07-27 21:05:00.738 - DEBUG: Can read: 140
    2012-07-27 21:05:00.738 - DEBUG: Amount of data: 3
    2012-07-27 21:05:00.738 - DEBUG: Name: image.png
    2012-07-27 21:05:00.738 - DEBUG: Dir: image.png
    2012-07-27 21:05:00.738 - DEBUG: Size: 32888
    2012-07-27 21:05:00.739 - DEBUG: Name:image.png Size:32888 dir:image.png
    2012-07-27 21:05:00.739 - DEBUG: Recieving second file!
    2012-07-27 21:05:00.739 - DEBUG: Can read: 0
    java.io.EOFException
    	at java.io.DataInputStream.readInt(Unknown Source)
    	at FileInfo.read(FileInfo.java:39)
    	at FileSendRecieve.recieveFile(FileSendRecieve.java:30)
    	at Client.run(Client.java:26)
    	at java.lang.Thread.run(Unknown Source)

  25. #25
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: EOFException

    Owh, the client seems to read everything already, since this is what happens on the server:
    2012-07-27 21:13:09.335 - SYSTEM: Logger initalized
    2012-07-27 21:13:09.335 - SYSTEM: Server starting up. . . 
    2012-07-27 21:13:09.340 - SYSTEM: Server started succesfully in: 0.04194304 seconds.
    2012-07-27 21:13:09.341 - SYSTEM: Server is now accepting clients. . . 
    2012-07-27 21:13:09.341 - SYSTEM: Waiting for client. . . 
    2012-07-27 21:13:12.372 - SYSTEM: Client connected Socket[addr=/127.0.0.1,port=49793,localport=8081]
    2012-07-27 21:13:12.374 - SYSTEM: Waiting for client. . . 
    2012-07-27 21:13:12.376 - DEBUG: Sending 3 amount of data
    2012-07-27 21:13:12.376 - DEBUG: Send The following data:
    2012-07-27 21:13:12.376 - DEBUG: Name:image.png Size:32888 dir:image.png
    2012-07-27 21:13:12.376 - DEBUG: Written: 68
    2012-07-27 21:13:12.376 - DEBUG: Sending 3 amount of data
    2012-07-27 21:13:12.377 - DEBUG: Send The following data:
    2012-07-27 21:13:12.377 - DEBUG: Name:image2.png Size:67145 dir:image2.png
    2012-07-27 21:13:12.377 - DEBUG: Written: 72
    So the client seems to recieve everything at once, where the server sends it nicely. But the second time the client expects to recieve something again even though the server already send everything. Then the server closes and the client will give an EOFException since the socket it's trying to read from is closed already trough the server. Could that be right?

Page 1 of 2 12 LastLast

Similar Threads

  1. EOFException on sending object
    By treshr in forum Java Networking
    Replies: 1
    Last Post: December 9th, 2011, 01:20 PM
  2. java.io.EOFException when sending object through socket
    By treshr in forum Java Networking
    Replies: 2
    Last Post: November 8th, 2011, 06:13 AM