gpt4 book ai didi

java - 如何解决java中的流关闭错误?

转载 作者:太空宇宙 更新时间:2023-11-04 09:01:05 25 4
gpt4 key购买 nike

我真的很感谢所有阅读本文并尝试帮助我的人,以下是我尝试为大学套接字编程项目的服务器类编写的代码:

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

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

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

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

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

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

最佳答案

一般来说,当出现 NullPointerException 时:

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

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

来自Jav Docs如果发生 I/O 错误并且 IOException 则读取可能会抛出 IOException可以给您指定的详细信息。稍后可以通过类 java.lang.Throwable 的 Throwable.getMessage() 方法检索错误消息字符串。

两点:

  • IOException 详细信息为您提供了什么?
  • 由于这是您的大学类(class),请尝试向您的同学或助教寻求帮助

关于java - 如何解决java中的流关闭错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/461826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com