传输少于整个文件的套接字

时间:2013-06-28 13:29:01

标签: java android sockets

我正在尝试使用Java中的套接字发送文件。问题是这样的: 假设有一个97kb的文件。它大约95.8kb并等待更多,但作者已经发送了所有97kb。

阅读:

FileOutputStream fout = new FileOutputStream(fl);
int counter = 0;
byte[] byt = new byte[8192];
BufferedInputStream bin = new BufferedInputStream(cli.InputStream());
int count = 0;

while((count = bin.read(byt)) > 0)
{
    counter = counter + count;
    Log.d("TINTERACT", String.valueOf(count) + " _" + String.valueOf(counter) + " _" +  String.valueOf(size));
    fout.write(byt, 0, count);
}

fout.flush();
fout.close();

写作时:

System.out.println("Starting writing");
FileInputStream fIn = new FileInputStream(path);
byte[] byt = new byte[8192];
BufferedInputStream bin = new BufferedInputStream(fIn);
BufferedOutputStream bout = new BufferedOutputStream(ser.OutputStream());

int count = 0, countr = 0;
while((count = bin.read(byt)) > 0)
{
    System.out.println(count);
    bout.write(byt, 0, count);
    countr = countr + count;
}

bout.flush();
System.out.println("sent " + countr + "End");
bin.close();

编写器完成发送字节总数而读取器不获取所有字节并循环等待它

2 个答案:

答案 0 :(得分:1)

从发送器读取的接收器循环在发送者关闭其套接字之前不会终止。

答案 1 :(得分:-1)

尝试使用:

while((count = bin.read(byt)) != -1)

而不是:

while((count = bin.read(byt))>0)