为什么抛出此异常以及如何从中恢复?

时间:2014-04-15 08:18:00

标签: java sockets network-programming java-io eofexception

我正在创建一个远程桌面截图应用程序。我在服务器上有两种方法1)从客户端读取图像2)读取客户端上运行的任务列表。但每次我尝试读取客户端的输入流时,都会抛出EOF。异常的stakctrace是

  

java.io.EOFException at   java.io.ObjectInputStream中的$ PeekInputStream.readFully(ObjectInputStream.java:2323)     在   java.io.ObjectInputStream中的$ BlockDataInputStream.readShort(ObjectInputStream.java:2792)     在   java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:799)     在java.io.ObjectInputStream。(ObjectInputStream.java:299)at   remoteserverclient.original.ScreenServer $ ServerThread.run(ScreenServer.java:254)

以下是服务器上抛出异常的代码

ObjectInputStream in = new ObjectInputStream(client.getInputStream());

                    Object obj = in.readObject();
                    if (obj instanceof Rectangle) {
                        CaptureScreen(obj, in);
                    } else if (obj instanceof String) {
                        CaptureList(in);
                    }

以下是客户端的完整代码

public class ScreenClient {

    static Socket server;

    public static void main(String[] args) throws Exception {
        try {


            while (true) {
                server = new Socket("localhost", 5494);
                BufferedReader bf = new BufferedReader(new InputStreamReader(server.getInputStream()));
                String s;
                s = bf.readLine();

                System.out.println(s);
                if (s.contains("execute")) {
                    new ClientMessageThread().start();
                }
                if (s.contains("getProcessList")) {
                    new ClientFetchProcessThread().start();
                }
            }
        } catch (Exception e) {
            System.err.println("Disconnected From server ->" + e.getMessage());
        }
    }

    public static class ClientMessageThread extends Thread {
      Socket server;
        public ClientMessageThread() {
          try {
              server=new Socket("localhost",5494);
          } catch (Exception ex) {
              ex.printStackTrace();
          }
        }



        @Override
        public void run() {
            try {


                BufferedImage screen;

                Robot robot = new Robot();
                Rectangle size = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

                screen = robot.createScreenCapture(size);

                int[] rgbData = new int[(int) (size.getWidth() * size.getHeight())];

                screen.getRGB(0, 0, (int) size.getWidth(), (int) size.getHeight(), rgbData, 0, (int) size.getWidth());



                OutputStream baseOut = server.getOutputStream();

                ObjectOutputStream out = new ObjectOutputStream(baseOut);

                out.writeObject(size);

                for (int x = 0; x < rgbData.length; x++) {

                    out.writeInt(rgbData[x]);

                }

                out.flush();
                server.close();
                //added new
            } catch (Exception e) {
                System.err.println("Disconnected From server ->" + e.getMessage());
            }
        }
    }

    public static class ClientFetchProcessThread extends Thread {
        Socket server;
        public ClientFetchProcessThread() {
          try {
              server=new Socket("localhost",5494);
          } catch (Exception ex) {
              ex.printStackTrace();
          }
        }
        public void run() {


            try {
                PrintWriter ps;

                System.out.println("\n\n********");
                StringBuilder builder = new StringBuilder("");
                String query = "tasklist";

                Runtime runtime = Runtime.getRuntime();
                InputStream input = runtime.exec(query).getInputStream();
                BufferedInputStream buffer = new BufferedInputStream(input);
                BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer));
                String line = "";
                ps = new PrintWriter(server.getOutputStream(), true);
                while ((line = commandResult.readLine()) != null) {
                    builder.append(line + "\n");

                    //byte[] responseClient=s.getBytes();


                    ps.write(builder.toString());
                    System.out.println(builder.toString());


                }
                server.close();
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您正在从客户端打印和写入端口5494的对象。服务器只读取对象。

将其整理出来。

答案 1 :(得分:0)

抛出的异常是EOFException(文件结束异常)。

ObjectInputStream在到达输入结尾时抛出EOFException。这是标准的行为。您是否正在捕获in.readObject()抛出的所有异常?

文档:

http://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html