如何解决java中的Stream Closed Error?

时间:2009-01-20 15:41:36

标签: java sockets

我非常感谢所有读过这篇文章并试图帮助我的人,以下是我正在为大学的套接字编程项目编写服务器类的代码:

import java.io.*;
import java.net.*;
import java.io.File;

class Server{
    public static void main (String[]args)throws IOException{
        ServerSocket socket1 = new ServerSocket (8000);
        while (true) {
            Socket incoming = socket1.accept();
            new newclass(incoming).start();
        }
    }
}

class newclass extends Thread implements Runnable {

    Socket incoming;

    public newclass(Socket incoming) {
        this.incoming = incoming;
    }

    public void run() {
        try {
            byte x = 0;
            String z;
            String s = "HTTP 1.0 200 Document follows";
            String s1 = "Bad request message";
            BufferedReader input = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            PrintWriter output = new PrintWriter(incoming.getOutputStream(), true);
            DataOutputStream sending = new DataOutputStream(incoming.getOutputStream());
            File directory = new File("C:\\Documents and Settings\\Ahmed\\Desktop\\bla\\Server");
            File[] files = directory.listFiles();
            int x1 = files.length;
            if ((x1 - 3) < 10) {
                boolean done = false;
                while (!done) {
                    String line = input.readLine();
                    System.out.println(line);
                    if (line.equals("BYE")) {
                        output.println("BYE");
                        done = true;
                    } else {
                        if (line.trim().substring(0, 3).equals("GET ")) {
                            if (line.equals("<javalogo.png> HTTP    1.0")) {
                                File f = new File("javalogo.png");
                                int size = (int) f.length();
                                if (f.exists() == true) {
                                    output.println(s);
                                    output.println(size);
                                    output.println("javalogo1.png");
                                    DataInputStream bytefile = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
                                    while (bytefile.available() != 0) {
                                        x = bytefile.readByte();
                                        sending.write(x);
                                    }
                                } else {
                                    System.out.println("Getting file from main server");
                                    Socket socket2 = new Socket("127.0.0.1", 8100);
                                    BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
                                    PrintWriter pOut = new PrintWriter(socket2.getOutputStream(), true);
                                    BufferedReader bIn = new BufferedReader(new InputStreamReader(socket2.getInputStream()));
                                    pOut.println("GET <javalogo.png> HTTP 1.0");
                                    String rep = bIn.readLine();
                                    if (rep.equals("HTTP 1.0 200 Document follows")) {
                                        int len = Integer.parseInt(bIn.readLine());
                                        String fname = bIn.readLine();
                                        File f1 = new File(fname);
                                        f1.createNewFile();
                                        FileOutputStream fos = new FileOutputStream(f1);
                                        DataInputStream dis = new DataInputStream(socket2.getInputStream());
                                        for (int i = 0; i < len; i++) {
                                            fos.write(dis.read());
                                        }
                                        fos.close();
                                    } else if (rep.equals("File does not exist")) {
                                        output.println("Sorry, but the file was neither found in the proxy server or the main server or the name is wrong.");
                                    }

                                }
                            }
                            File f2 = new File("javalogo.png");
                            if (f2.exists() == true) {
                                int size = (int) f2.length();
                                output.println(s);
                                output.println(size);
                                output.println("javalogo.png");
                                DataInputStream bytefile = new DataInputStream(new BufferedInputStream(new FileInputStream(f2)));
                                while (bytefile.available() != 0) {
                                    x = bytefile.readByte();
                                    sending.write(x);
                                }
                            }
                        } else {
                            System.out.println(s1);
                            output.println(s1);
                        }
                    }
                }
                incoming.close();

            }
            output.println("Connecting to main server");
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

现在我不明白为什么在我运行下面的客户端时出现错误 我得到这个非常奇怪的错误,其中缓冲的读取器正确地从用户读取第一行但是第二行它给了我一个空的异常,好像客户端写了null或者其他东西,我得不到它。

无论如何,这是客户端代码,如果有人可以帮助我,我会非常感激。

import java.net.*;
import java.io.*;


public class Client {

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

        Socket socket1 = new Socket("127.0.0.1", 8000);

        BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pOut = new PrintWriter(socket1.getOutputStream(), true);
        BufferedReader bIn = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
        String cmd;
        String rep;
        while (true) {
            cmd = bUsr.readLine();
            pOut.println(cmd);

            System.out.println(rep = bIn.readLine());

            if (cmd.equals("BYE") || cmd.equals("END"))
                break;
            else if (rep.equals("HTTP 1.0 200 Document follows")) {
                int len = Integer.parseInt(bIn.readLine());
                String fname = bIn.readLine();
                File f = new File(fname);
                f.createNewFile();
                FileOutputStream fos = new FileOutputStream(f);
                DataInputStream dis = new DataInputStream(socket1.getInputStream());
                for (int i = 0; i < len; i++) {
                    fos.write(dis.read());
                }

                fos.close();
                System.out.println("Success");

            } else if (rep.equals("Connecting to main server")) {
                Socket socket1 = new Socket("127.0.0.1", 8100);
                BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
                PrintWriter pOut = new PrintWriter(socket1.getOutputStream(), true);
                BufferedReader bIn = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
                String cmd;
                String rep;
                while (true) {
                    cmd = bUsr.readLine();
                    pOut.println(cmd);

                    System.out.println(rep = bIn.readLine());

                    if (cmd.equals("BYE") || cmd.equals("END"))
                        break;
                    else if (rep.equals("HTTP 1.0 200 Document follows")) {
                        int len = Integer.parseInt(bIn.readLine());
                        String fname = bIn.readLine();
                        File f = new File(fname);
                        f.createNewFile();
                        FileOutputStream fos = new FileOutputStream(f);
                        DataInputStream dis = new DataInputStream(socket1.getInputStream());
                        for (int i = 0; i < len; i++) {
                            fos.write(dis.read());
                        }

                        fos.close();
                        System.out.println("Success");
                    }
                }
            }

            bIn.close();
            pOut.close();
            socket1.close();
        }
    }

这是第一次在这个网站上询问任何内容,所以如果我做错了什么,我会非常乐意改变我写的内容。

顺便说一句,服务器中声明“从主服务器获取文件”的部分是服务器本身成为主服务器的客户端的部分,从中获取文件并将其发送到客户端,I没有添加主服务器代码,因为代码太多,但基本上它与没有if条件的服务器相同,仅限于目录中的10个文件。

5 个答案:

答案 0 :(得分:1)

通常,当存在NullPointerException时:

  • 您尚未实例化您的对象
  • 您的对象已被销毁(已关闭),因此不存在
  • 您的投射无效
  • 您的代码已覆盖您的对象指针

您需要检查堆栈转储以查看哪些是正确的。

如果发生I / O错误,Jav Docs可以为IOException提供指定的详细消息,则{{3}}可以抛出IOException。稍后可以通过类java.lang.Throwable的Throwable.getMessage()方法检索错误消息字符串。

两点:

  • IOException详细信息为您提供了什么?
  • 由于这是针对您的大学课程,请尝试向您的同学或TA寻求帮助

答案 1 :(得分:0)

如果我说我完全理解你的代码正在做什么,我会撒谎 - 但是当你从一个你希望包含数据的流中读取时你得到空数据时,输出流可能没有'刷新'数据。

确保在写完后输出流上调用flush()方法

答案 2 :(得分:0)

Ahemd,我在课程中看到了许多潜在的错误和一些实际的错误,但代码太复杂了,我无法专注于你所看到的问题。如果您仍然遇到网络错误,请尝试将代码和客户端和服务器都尽可能减少以进行通信(发送/读取一行)。如果这不适合你,发布这些课程,我们将能够比在这里发布的更大的课程更快地看到问题。

祝你好运。

答案 3 :(得分:0)

如果从BufferedReader.readLine()获取null,则意味着您已经到达输入的末尾,因为它在此方法的javadoc中声明。

答案 4 :(得分:0)

找到错误解决方案的最佳方法是查看跟踪。没有它我会在你的代码中看到多个错误。

问候。