android socket eofexception,Java socket EOFException

问题

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.(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 ?

回答1:

FileNotFoundException has two meanings:

On input, the file wasn't found, couldn't be opened due to permissions, etc.

On output, the target directory wasn't found, couldn't be written into due to permissions, etc.

For example if the filename you are sending contains a directory that doesn't exist at the server, you will get FileNotFoundException from new 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() and write() 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.

来源:https://stackoverflow.com/questions/50666927/java-socket-eofexception

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值