0

我正在尝试使用 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();

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

4

2 回答 2

1

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

于 2013-06-28T18:31:57.830 回答
-1

尝试使用:

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

代替:

while((count = bin.read(byt))>0)
于 2013-06-28T13:42:58.693 回答