【发布时间】:2016-12-18 04:35:47
【问题描述】:
我正在尝试将 apk 文件从一台设备发送到另一台设备。基本上,要发送的用户有一个带有他安装的应用程序(非系统应用程序)的网格视图,当用户按下其中一个应用程序的图标时,它会将其发送到另一台设备。该文件被缓冲成更小的数据包。
我正在做一些系统输出来检查正在发生的事情,同时发送方设备正在发送 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,但无需双缓冲。
标签: android sockets io buffer file-transfer