我正在嘗試將apk文件從一個設備發送到另一個設備。基本上,要發送的用戶具有已安裝的應用程序(非系統應用程序)的gridview,並且當用戶按下其中一個應用程序的圖標時,它會將其發送到其他設備。該文件被緩衝成較小的數據包。BufferInputStream不讀取最後一個數據包
我正在做一些系統來檢查發生了什麼,並且發送設備正在發送X個數據包,同時接收方接收的數據包少於X個數據包(它依賴於正在發送的應用程序,所以這就是爲什麼我寫X )。我認爲這是最後一次失蹤。
我的緩衝區的長度是4096,「最後一個數據包」只有2115個。在接收端,它不會拋出讀寫文件後出現的系統信息。所以程序在閱讀指令時被卡住了。 我的問題是爲什麼最後一個數據包沒有被傳輸?
這是我的代碼。
客戶端 - apk的發送者。
public class SendThread implements Runnable{
@Override
public void run() {
try {
if(clientSocket==null){
clientSocket=new Socket(serverIP,porto);
System.out.println("client comm - Created the client socket..");
}
System.out.println(" client comm, i am here in the send thread, going to create the outputstream object");
if(out==null){
out = new ObjectOutputStream(clientSocket.getOutputStream());
System.out.println(" Client COMM SEND THREAD - JUST created the outputstream .");
}
File apkToSend=new File(filePath);
byte[] buffer = new byte [4096];
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(apkToSend));
int count;
int total=0;
while((count=bis.read(buffer,0,4096))!=-1){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
out.write(buffer,0,count);
total+=count;
out.flush();
System.out.println("Client Comm send thread - already sent this ammount : "+total);
}
bis.close();
out.flush();
System.out.println("clientComm send thread - Just sent the message ! i am after the out.writeOBject. Not saving it to a string yet.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
現在在服務器端 - 接收文件的設備..
public class ReceiveThread implements Runnable{
@Override
public void run() {
try {
if(socket==null){
socket=serverSocket.accept();
System.out.println("serverComm - accepted the socket from the client.");
out=new ObjectOutputStream(socket.getOutputStream());
out.writeObject("Hello client, i the server, sending you this message because i want to. SO that you unlock from the input stream creation");
System.out.println("SErverComm Created the output stream and sent a HEllo message to the client ");
}
if(in==null){
in = new ObjectInputStream(socket.getInputStream());
}
while(!serverSocket.isClosed()){
try {
if(!handshakeDone){
System.out.println("server comm receive thread - INSIDE THE WHILE CICLE, before the in.readObject");
String message = (String) in.readObject();
System.out.println("ServercomM receive thread - just did the handshake message control");
handshakeDone=true;
}else{
System.out.println("server comm receive thread - INSIDE THE WHILE CICLE, before the in.readObject");
File apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/testeReceiveServerComm.apk");
byte[] buffer = new byte [4096];
BufferedInputStream bis=new BufferedInputStream(in);
FileOutputStream fos=new FileOutputStream(apkReceived);
int count=0;
int total=0;
while((count=bis.read(buffer,0,4096))!=-1){
fos.write(buffer,0,count);
total+=count;
System.out.println("Server Comm receive thread - already received this ammount : "+total);
}
;
System.out.println("Already received everything ! ");
fos.flush();
bis.close();
fos.close();
System.out.println(" server comm receive thread - already read the object !!!!!!!!!!!!!!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
沒有理由使用BifferInputStream,因爲您一次只能讀取4092個字節來緩衝自己。您可以嘗試將其提高到8192,但不需要雙緩衝區。 – lionscribe