Java套接字EOFException [英] Java socket EOFException

查看:96
本文介绍了Java套接字EOFException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RMI和套接字在客户端集之间传输文件。问题是当运行下面的代码时有时我在客户端获得此异常:

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 :

客户:

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();

服务器:

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 有两个含义:


  1. 输入时,找不到文件,由于权限无法打开等等。

  2. 输出时,找不到目标目录,由于权限等原因无法写入

  1. On input, the file wasn't found, couldn't be opened due to permissions, etc.
  2. On output, the target directory wasn't found, couldn't be written into due to permissions, etc.

例如,如果您发送的文件名包含服务器上不存在的目录,您将从<$获得 FileNotFoundException c $ c> new FileOutputStream 。

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.

但是:


  • 您不需要对象流,甚至数据输入/输出流。你只使用基本的 read() write()方法,所以你也可以使用套接字流直接。

  • 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.

你应该使用比1024字节更大的缓冲区,比如8192或其倍数。

You should use a larger buffer than 1024 bytes, say 8192 or a multiple thereof.

您可以通过我对这个问题

这篇关于Java套接字EOFException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆