Results 1 to 3 of 3
  1. #1
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default EOFException randomly

    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 i get the error:
    Java Code:
    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.

    Java Code:
    	/*
    	 * 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();
    	}
    My write and read functions
    read and writestring function:
    Java Code:
    	/**
    	 * 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]);
    		}
    	}
    A commonly class to send and recieve file info:
    Java Code:
    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:

    Java Code:
    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();
    		Logger.getInstance().system("Server starting up. . . ");
    	}
    
    	public void startService() {
    		try {
    			ss = new ServerSocket(8081);
    			run = true;
    		} catch (IOException e) {
    			Logger.getInstance().error(e);
    			Logger.getInstance()
    					.error("Port already in use, or no acces to port. Program will now exit.");
    			System.exit(0);
    		}
    		Logger.getInstance().system(
    				"Server started succesfully in: "
    						+ ((System.nanoTime() - runTime) / Math.pow(10, 9))
    						+ " seconds.");
    		this.start();
    	}
    
    	public void run() {
    		Logger.getInstance().system("Server is now accepting clients. . . ");
    		while (run) {
    			Logger.getInstance().system("Waiting for client. . . ");
    			try {
    				cs = ss.accept();
    				Logger.getInstance().system("Client connected " + cs);
    				new Thread(new ClientHandler(cs)).start();
    			} catch (Exception e) {
    				Logger.getInstance().error(e);
    				Logger.getInstance().error("Something went wrong while accepting client. Program will now exit");
    				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();
    				while(true);
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void main(String args[]) {
    		Server server = new Server();
    		server.startService();
    	}
    }
    and my client

    Java Code:
    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();
    		}
    	}
    }

  2. #2
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: EOFException randomly

    Nobody? I'd really like to continue on this project. But i cant find my problem :(

  3. #3
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: EOFException randomly

    Is it java? Or is it this forum that is dieing? :(

Similar Threads

  1. EOFException
    By Pojahn_M in forum New To Java
    Replies: 1
    Last Post: 03-02-2012, 11:18 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, 08:53 PM
  3. java.io.EOFException using readObject
    By yotamoo in forum Advanced Java
    Replies: 0
    Last Post: 12-31-2010, 11:00 AM
  4. Object Input Stream EOFException
    By FlyNn in forum New To Java
    Replies: 1
    Last Post: 12-18-2010, 01:33 PM
  5. EOFException
    By coyne20 in forum Introductions
    Replies: 0
    Last Post: 02-19-2009, 03: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
  •