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

ご覧のとおり、問題を解決するために、サーバー コードで 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;
}
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 に答える