1

我对客户端服务器图像传输有奇怪的行为。当前服务器总是在从缓冲区中读取 32768 字节时停止,总文件大小为 36556 字节。while 循环没有结束,但它不会打印其中的任何 print 语句,也没有抛出异常。我尝试了几种不同大小的字节缓冲区,甚至大于图像大小也不能解决问题。

客户代码:

private static void writePhoto(Socket socket) throws IOException {

    OutputStream outputStream = new   BufferedOutputStream(socket.getOutputStream());

    String file_path = "E:\\Eclipse\\Workspace\\DNI_PsuedoClient\\" +
            "src\\main\\resources\\BuckyBadger.jpg";
    try {
        InputStream stream = new FileInputStream(file_path);
        System.out.println(stream.available());
        try {
            byte[] buffer = new byte[1024];
            int readData;
            int i = 1; 
            while((readData=stream.read(buffer))!=-1){
                System.out.println(readData + " " + i);
            outputStream.write(buffer,0,readData);
            i++;
            }
            System.out.println("done writing to buffer");
        }catch (IOException e) {
              System.err.println("File Error");
              e.printStackTrace();
              System.exit(-1);
        }finally {
            stream.close();
        }
    } finally {
    }
}

服务器代码

private static java.io.File createFile(Socket client) throws IOException {

    System.out.println("Starting to create file");
    InputStream stream = new BufferedInputStream(client.getInputStream());

    System.out.println("1");
    // Create file from the inputStream
    java.io.File Recieved_File = new java.io.File(thread_ID + ".jpg");
    System.out.println(thread_ID + ".jpg");
    System.out.println("2");

    try {
       OutputStream outputStream = new FileOutputStream(Recieved_File);
       System.out.println("3");

       try {
          byte[] buffer = new byte[1024];
          System.out.println("4");
          int readData;
          int i = 1;

          while ((readData = stream.read(buffer)) != -1) {
              System.out.println("start");
              outputStream.write(buffer, 0, readData);
              System.out.println(readData + " " + i + " " + (i * readData));
              i++;
              System.out.println("end while loop");
          }
       } finally {
         System.out.println("5");
         outputStream.close();
         System.out.println("6");
         }
     } finally {
   }
   System.out.println("Created file");
   return Recieved_File;
}

如您所见,我已经多次尝试使用服务器代码中的打印语句来试图找出问题所在。任何见解都会非常有帮助。

为了进一步参考,我有完全相同的服务器代码用于与 Windows 手机的交互,手机拍摄照片并将其上传到服务器。我现在正在更改代码以在多个线程上运行,并首先使用模拟交互的 Java 客户端在 Java 中对其进行测试。java 客户端正在尝试将存储在驱动器上的照片发送到服务器。客户端似乎工作正常,因为它将整张照片放入缓冲区。

我写了一个协议,这里是:

public DNI_Protocol(Socket socket, String threadID) {
    client = socket;
    thread_ID = threadID;
}

public String processInput(String theInput) {

    String theOutput = null;
    if (state == WAITING) {
      System.out.println(theInput);
      if (theInput.equals("Upload")) {
        state = UPLOAD_PHOTO;
        theOutput = "Send Photo";
      } else if (theInput == "View") {
        state = VIEW;
        theOutput = "Send Request";
      } else {
        theOutput = "Waiting";
      }
    } else if (state == UPLOAD_PHOTO) {
      // if (theInput != "Sending Photo") {
      // TODO: Throw a state exception with a message
      // return "exit";
      // }
      setupDrive();
      try {
        Image_Handle = createFile(client);
        System.out.println("Uploading file");
        uploadedFile = Drive_Interface.uploadFile(false, Image_Handle, drive);
        System.out.println("file Uploaded");
        google_ID = uploadedFile.getId();
        System.out.println(google_ID);
        Image_Handle.delete(); // We are done with the file so we can delete it
        System.out.println("deleted file");
      } catch (IOException e) {
        e.printStackTrace();
      }
      theOutput = "Send Keywords";
      state = UPLOAD_KEYWORDS;
    } else if (state == UPLOAD_KEYWORDS) {
      if (theInput != "Sending Keywords") {
        // TODO: Throw a state exception with a message
        return "exit";
      }
      // TODO: Add code to get keyword arraylist and upload information to
      // the database
      theOutput = "exit";
    }
    return theOutput;
}
4

1 回答 1

4

您的服务器代码是这样说的:

      while ((readData = stream.read(buffer)) != -1) {
          System.out.println("start");
          outputStream.write(buffer, 0, readData);
          System.out.println(readData + " " + i + " " + (i * readData));
          i++;
          System.out.println("end while loop");
      }

这会一直从套接字读取,直到套接字关闭或服务器出错,没有其他原因read会返回-1。由于这两种情况都没有发生,因此服务器将停留在此while循环中。您根本没有编写任何代码来处理文件的结尾。

如果服务器知道它将读取特定数量的字节,则它需要在读取该数量的字节后跳出while循环。如果客户端要通过关闭套接字或关闭 TCP 连接的一个方向来告诉服务器它已完成文件发送,那么客户端需要代码来执行此操作。否则,服务器将永远等待。

于 2012-11-15T13:02:24.870 回答