Results 1 to 3 of 3
Thread: EOFException randomly
- 07-22-2012, 02:15 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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:
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: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)
My write and read functionsJava 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(); }
read and writestring function:
A commonly class to send and recieve file info: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]); } }
My server: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); } }
and my clientJava 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(); } }
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(); } } }
- 07-24-2012, 04:52 PM #2
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: EOFException randomly
Nobody? I'd really like to continue on this project. But i cant find my problem :(
- 07-27-2012, 05:20 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
EOFException
By Pojahn_M in forum New To JavaReplies: 1Last Post: 03-02-2012, 10:18 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