DataInputStream を使用して、Android クライアントからこの Java デスクトップ サーバーに送信される int と long を取得します。その後、Android クライアントから pdf ファイルを受信します。クライアントからサーバーに送信される合計 3 つのファイル。問題は、他の方向に送信する場合です。
while ループの直後に入力ストリームと出力ストリームの両方を閉じる必要があります。そうしないと、pdf ファイルが破損し、プログラムが停止して while ループでスタックし、スレッドの最後まで実行を継続できなくなります。
入力ストリームと出力ストリームを閉じる必要がある場合、ソケットが閉じられます。同じソケットを再度開くにはどうすればよいですか?
ファイルがサーバーによって安全に受信されたという確認を送信するために、サーバーがpdfファイルを受信したというメッセージをAndroidクライアントに送信する必要があるため、同じソケットを再度開く必要があります。
同じ単一のJavaサーバーに複数のAndroidクライアントが接続されているため、メッセージをクライアントに送り返すには同じソケットが必要だと思います。ソケットがないと、どのクライアントにメッセージを送信するかを判断するのが難しくなります。
byte[] buffer = new byte[fileSizeFromClient];
while((count = dis.read(buffer)) > 0){
bos.write(buffer, 0, count);
}
dis.close(); // closes DataInputStream dis
bos.close(); // closes BufferedOutputStream bos
編集:
クライアントからのコード
dos.writeInt((int)length); // sends the length as number bytes is file size to the server
dos.writeLong(serial); // sends the serial number to the server
int count = 0; // number of bytes
while ((count = bis.read(bytes)) > 0) {
dos.write(bytes, 0, count);
}
dos.close(); // need to close outputstream or result is incomplete file sent to server
// and the server hangs, stuck on the while loop
// if dos is closed then the server sends error free file