无法通过TCP连接传输图像

时间:2012-11-15 12:12:34

标签: java tcp

我对客户端服务器图像传输有奇怪的行为。目前,当服务器从缓冲区读取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;
   }

正如您所看到的,我已尝试使用服务器代码中的print语句尝试找出问题。任何见解都会非常有用。

为了进一步参考,我有一个完全相同的服务器代码,用于与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;

}

2 个答案:

答案 0 :(得分:3)

您的服务器代码说明了这一点:

      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连接的一个方向告诉服务器它已完成发送文件,那么客户端需要代码来执行该操作。否则,服务器将永远等待。

答案 1 :(得分:0)

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

此代码在32768字节处停在完全相同的位置;