I use RMI and socket to transfer files between a client set. The problem is when running the code below sometimes i get this exception in client side :
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2671)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3146)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:858)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:354)
at javafxcyberwind.serverSocket.run(serverSocket.java:38)
at java.lang.Thread.run(Thread.java:748)
This is a sample of my code, the exceptions are in comment lines :
Client :
Server_MethodsPEER S = (Server_MethodsPEER) Naming.lookup("rmi://" + ip + ":" + port + "/" + email);
Server_Methods R = S.peer();
R.uploadToServer(fileName);
Socket s = new Socket(ip, R.getServerSocketPort());
File fres = new File(crep + fileName);
FileInputStream inf = new FileInputStream(fres);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
byte buf[] = new byte[1024];
int nn;
while ((nn = inf.read(buf)) != -1) {
out.write(buf, 0, nn);
}
out.close();
inf.close();
s.close();
fres.delete();
Server :
public class serverSocket implements Runnable {
private final ServerSocket socketserver;
private Socket socket;
private final String ff;
private final String rp;
public serverSocket(ServerSocket s, String file, String rep) {
socketserver = s;
ff = file;
rp = rep;
}
@Override
public void run() {
try {
socket = socketserver.accept();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());//EOFException here
FileOutputStream out = new FileOutputStream(new File(rp + ff));
byte buf[] = new byte[1024];
int n;
while ((n = in.read(buf)) != -1) {
out.write(buf, 0, n);
}
in.close();
out.close();
socket.close();
socketserver.close();
} catch (IOException e) {
System.err.println("socket err");
}
}
}
//ServerIMPL
ServerSocket sv;
public void uploadToServer(String fileName) throws RemoteException {
try {
sv = new ServerSocket(0);
} catch (IOException ex) {
Logger.getLogger(Cloud_ServeurIMPL.class.getName()).log(Level.SEVERE, null, ex);
}
Thread t = new Thread(new serverSocket(sv, file, crep));
t.start();
Client3_MethodsPEER M = (Client3_MethodsPEER) Naming.lookup("rmi://" + ip + ":" + port + "/" + email);
Client3_Methods U = M.peer();
U.uploadToServer(fileName);
Socket s = new Socket(ip, U.getServerSocketPort());
File fres = new File(crep + fileName);
FileInputStream inf = new FileInputStream(fres);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
byte buf[] = new byte[1024];
int nn;
while ((nn = inf.read(buf)) != -1) {
out.write(buf, 0, nn);
}
out.close();
inf.close();
s.close();
fres.delete();
U.operation();//FileNotFoundException here (use the received files as arguments)
}
Everything seems to be fine. Why is this happening and how to solve it ?
FileNotFoundException
has two meanings:For example if the filename you are sending contains a directory that doesn't exist at the server, you will get
FileNotFoundException
fromnew FileOutputStream
.There isn't enough (i.e. any) information in your question to say any more about that.
However:
You don't need object streams for this, or even data input/output streams. You're only using the basic
read()
andwrite()
methods, so you may as well just use the socket streams directly.You should use a larger buffer than 1024 bytes, say 8192 or a multiple thereof.
You can send multiple files per connection via the technique given in my answer to this question.