如何解决java中的流关闭错误?
我真的很感谢每一位阅读本文并试图帮助我的人,以下是我试图为大学socket编程项目的服务器课程编写的代码:如何解决java中的流关闭错误?,java,sockets,Java,Sockets,我真的很感谢每一位阅读本文并试图帮助我的人,以下是我试图为大学socket编程项目的服务器课程编写的代码: 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);
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.io.*;
导入java.net。*;
导入java.io.File;
类服务器{
公共静态void main(字符串[]args)引发IOException{
ServerSocket socket1=新的ServerSocket(8000);
while(true){
套接字传入=socket1.accept();
新建newclass(传入).start();
}
}
}
类newclass扩展线程实现可运行{
插座输入;
公共newclass(套接字传入){
this.incoming=传入;
}
公开募捐{
试一试{
字节x=0;
字符串z;
String s=“HTTP 1.0 200文档如下”;
String s1=“错误的请求消息”;
BufferedReader输入=新的BufferedReader(新的InputStreamReader(incoming.getInputStream());
PrintWriter输出=新的PrintWriter(incoming.getOutputStream(),true);
DataOutputStream发送=新的DataOutputStream(incoming.getOutputStream());
文件目录=新文件(“C:\\Documents and Settings\\Ahmed\\Desktop\\bla\\Server”);
File[]files=目录.listFiles();
int x1=files.length;
如果((x1-3)<10){
布尔完成=假;
而(!完成){
String line=input.readLine();
系统输出打印项次(行);
如果(行等于(“BYE”)){
输出println(“BYE”);
完成=正确;
}否则{
if(line.trim()子字符串(0,3).equals(“GET”)){
if(line.equals(“HTTP 1.0”)){
文件f=新文件(“javalogo.png”);
int size=(int)f.length();
如果(f.exists()==true){
输出。打印项次;
输出.println(大小);
println(“javalogo1.png”);
DataInputStream bytefile=新的DataInputStream(新的BufferedInputStream(新的FileInputStream(f));
while(bytefile.available()!=0){
x=bytefile.readByte();
发送。写入(x);
}
}否则{
System.out.println(“从主服务器获取文件”);
插座插座2=新插座(“127.0.0.1”,8100);
BufferedReader bUsr=新的BufferedReader(新的InputStreamReader(System.in));
PrintWriter pOut=新的PrintWriter(socket2.getOutputStream(),true);
BufferedReader bIn=新的BufferedReader(新的InputStreamReader(socket2.getInputStream());
println(“获取HTTP 1.0”);
字符串rep=bIn.readLine();
if(rep.equals(以下简称“HTTP 1.0 200文档”)){
int len=Integer.parseInt(bIn.readLine());
字符串fname=bIn.readLine();
文件f1=新文件(fname);
f1.createNewFile();
FileOutputStream fos=新的FileOutputStream(f1);
DataInputStream dis=新的DataInputStream(socket2.getInputStream());
对于(int i=0;i
现在我不明白为什么在运行以下客户端时会出现错误。我得到了一个非常奇怪的错误,缓冲读取器正确地读取了用户的第一行,但是第二行给了我一个null异常,好像客户端写了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();
}
}
import java.net.*;
英普