Java 软件导致的连接中止:套接字写入错误
我有一个例子,客户端在与服务器建立连接后,收到一个文件,当使用相同的连接时(持久),我最终得到上面提到的这个错误。 下面是实现的代码:Java 软件导致的连接中止:套接字写入错误,java,tcp,tcpclient,Java,Tcp,Tcpclient,我有一个例子,客户端在与服务器建立连接后,收到一个文件,当使用相同的连接时(持久),我最终得到上面提到的这个错误。 下面是实现的代码: Scanner in = new Scanner(file); this.clientSocket = new Socket(this.host,this.port); this.os = new DataOutputStream(this.clientSocket.getOutputStream()); this.is = this.clientSock
Scanner in = new Scanner(file);
this.clientSocket = new Socket(this.host,this.port);
this.os = new DataOutputStream(this.clientSocket.getOutputStream());
this.is = this.clientSocket.getInputStream();
while(in.hasNextLine()){
newFile = in.nextLine();
System.out.println(newFile);
this.os.writeBytes(newFile + '\n');
this.os.flush();
scanFileList();
writeFile();
}
服务器端实现是:
final class HttpRequest implements Runnable {
final static String CRLF = "\r\n";
Socket socket;
static String dir;
BufferedOutputStream outToClient = null;
// Constructor
public HttpRequest(Socket socket) throws Exception {
this.socket = socket;
dir = "C:\\Users\\";
}
// Implement the run() method of the Runnable interface.
public void run() {
try {
// Get a reference to the socket's input and output streams.
InputStream is = socket.getInputStream();
outToClient = new BufferedOutputStream(socket.getOutputStream());
processRequest(is,outToClient);
} catch (Exception e) {
System.out.println(e);
}
}
private void processRequest(InputStream is,BufferedOutputStream os) throws Exception {
// Set up input stream filters.
BufferedReader br = new BufferedReader(new InputStreamReader(is));
// Get the request line of the HTTP request message.
String fileName = br.readLine();
// Prepend a "." so that file request is within the current directory.
System.out.println(fileName);
// Open the requested file.
File myFile = null ;
boolean fileExists = true ;
myFile = new File(dir + fileName);
FileInputStream fis = null ;
try {
fis = new FileInputStream(dir + fileName);
System.out.println(fis);
} catch (FileNotFoundException e) {
fileExists = false ;
}
// Debug info for private use
System.out.println("Incoming!!!");
// Send the entity body.
if (fileExists) {
sendBytes(myFile, os);
//fis.close();
}
// Close streams and socket.
is.close();
os.close();
br.close();
socket.close();
}
private static void sendBytes(File myFile,
BufferedOutputStream os) throws Exception {
// Construct a 1K buffer to hold bytes on their way to the socket.
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
// Copy requested file into the socket's output stream.
try {
fis = new FileInputStream(myFile);
} catch (FileNotFoundException ex) {
// Do exception handling
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(mybytearray, 0, mybytearray.length);
os.write(mybytearray, 0, mybytearray.length);
os.flush();
// File sent, exit the main method
return;
} catch (IOException ex) {
// Do exception handling
}
}
}
当客户端的程序试图通过以下方式写入服务器时会发生错误:this.os.writebytes(newFile+/n)
这在你的代码中做什么
is.close();
os.close();
br.close();
socket.close();
在处理每个请求后,您是否明确地关闭了所有内容?您说,这是一个持久连接实现吗?这在您的代码中做了什么
is.close();
os.close();
br.close();
socket.close();
在处理每个请求后,您是否明确地关闭了所有内容?你说,这是一个持久连接实现?我不打算详细阅读你的代码(见上面的评论…),但它显然有些奇怪/错误 简单地说,如果您要与基于HTTP的服务器通信,您不能只打开一个套接字就写东西。客户端必须创建格式良好的HTTP请求,并处理返回的HTTP响应
客户端发生异常,因为服务器端。。。事实上你的代码。。。已关闭另一端的连接。我不打算详细阅读您的代码(请参阅上面的注释…),但它显然有些奇怪/错误 简单地说,如果您要与基于HTTP的服务器通信,您不能只打开一个套接字就写东西。客户端必须创建格式良好的HTTP请求,并处理返回的HTTP响应
客户端发生异常,因为服务器端。。。事实上你的代码。。。已关闭另一端的连接。为什么我不能向服务器发送请求,以便在循环中进行第二次运行?我只得到一个文件。如果您的代码格式/缩进正确,我可能会花时间尝试阅读它。但事实上。。。抱歉。为什么我不能向服务器发送循环中第二次运行的请求?我只得到一个文件。如果您的代码格式/缩进正确,我可能会花时间尝试阅读它。但事实上。。。对不起,你好,斯蒂芬,我已经格式化了代码。谢谢你的帮助。我看到了错误。默认情况下,我会在不检查连接类型的情况下关闭套接字。这是真的吗?问题是你正在关闭连接。我也不明白你为什么叫这个HTTP,因为它与真正的HTTP协议没有任何相似之处。谢谢你的帮助。我看到了错误。默认情况下,我会在不检查连接类型的情况下关闭套接字。这是真的吗?问题是你正在关闭连接。我也不明白你为什么叫这个HTTP,因为它和真正的HTTP协议没有任何相似之处。