1

我有一个案例,客户端在与服务器建立连接后接收一个文件,并且当使用相同的连接(持久)时,我最终得到了上面提到的这个错误。下面是实现的代码:

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

  Testfile01.bmp

  writing

  saved

  Testfile02.bmp

Exception in thread "main" java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.writeBytes(Unknown Source)
at TCPClientPersistentNp.openSocket(TCPClientPersistentNp.java:53)
at TCPClient.main(TCPClient.java:66)
4

2 回答 2

2

这是在你的代码中做什么?

is.close();
os.close();
br.close();
socket.close();

您在处理每个请求后明确关闭所有内容?你说这是一个持久连接的实现吗?

于 2012-04-15T09:49:43.227 回答
2

我不会尝试详细阅读您的代码(请参阅上面的评论...),但显然有一些非常奇怪/错误的地方。

简而言之,如果你要与基于 HTTP 的服务器通信,你不能只打开一个套接字并写东西。您的客户端必须创建格式良好的 HTTP 请求,并处理返回的 HTTP 响应。

客户端的异常正在发生,因为服务器端......实际上是您的代码......已经关闭了另一端的连接。

于 2012-04-15T09:51:17.657 回答