DataInputStream接收不准确

时间:2014-07-05 06:23:57

标签: java sockets datainputstream

这个问题真让我头疼。非常简单的场景。使用 DataInputStream 通过TCP发送和接收文件。

这是写方法

private boolean send(File file)
{
    try {
        FileInputStream fileIStream = new FileInputStream(file);
        BufferedInputStream bufferedIStream = new BufferedInputStream(fileIStream);
        DataInputStream dataIStream = new DataInputStream(bufferedIStream);
        byte [] buffer = new byte [sendBufferSize];

        dataOStream.writeInt(ID);
        dataOStream.writeUTF(file.getName());
        dataOStream.writeLong(file.length());
        dataOStream.writeUTF(data1);
        dataOStream.writeUTF(data2);
        dataOStream.writeInt(number1);
        //dataOStream.flush(); tried to flush here if writing data might be the problem

        int write, total=0;
        //Starting to send
        while((write = dataIStream.read(buffer)) >= 0)
        {
            dataOStream.write(buffer);
            total+=write;
            System.out.println("size: " + file.length() + "  write: " + write + "  total: " + total);
        }
        dataIStream.close();
        dataOStream.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

这是输出

size: 1830918  write: 8192  total: 8192
size: 1830918  write: 8192  total: 16384  
size: 1830918  write: 8192 total: 24576  
size: 1830918  write: 8192  total: 32768  
size: 1830918  write: 8192  total: 40960  
size: 1830918  write: 8192  total: 49152 
size: 1830918  write: 8192  total: 57344  
size: 1830918  write: 8192  total: 65536  
...............  
............... 
...............  
size: 1830918  write: 8192  total: 1802240  
size: 1830918  write: 8192  total: 1810432  
size: 1830918  write: 8192  total: 1818624  
size: 1830918  write: 8192  total: 1826816  
size: 1830918  write: 4102  total: 1830918

到目前为止,一切似乎都很好。问题在于阅读数据。

以下是我的阅读方式

private boolean receiveBinary()
{
    try {
        final int ID = dataIStream.readInt();

            final String fileName = dataIStream.readUTF();
            final long fileSize = dataIStream.readLong();
            final String data1 = dataIStream.readUTF();
            final String data2 = dataIStream.readUTF();
            final int number1 = dataIStream.readInt();
            //I printed out these data and everything is fine until here. No corruption or no missing.

            int read, totalRead = 0;
            File outputFile = createFile();
            OutputStream outputStream = new FileOutputStream(outputFile);

            try {
                byte [] readBuffer = new byte[readBufferSize];
                while ((read = dataIStream.read(readBuffer)) > 0) {
                    outputStream.write(readBuffer);
                    readBuffer = new byte[readBufferSize];
                    totalRead += read;
                    System.out.println("size: " + fileSize + "  read: " + read + "  total: " + totalRead);
                    if (totalRead == fileSize) {
                        outputStream.flush();
                        outputStream.close();
                        break;
                    }
                }
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
    }
    catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

这是接收

的输出
size: 1830918  read: 34228  total: 34228
size: 1830918  read: 2920  total: 37148
size: 1830918  read: 3812  total: 40960
size: 1830918  read: 1460  total: 42420
size: 1830918  read: 6732  total: 49152
size: 1830918  read: 2920  total: 52072
size: 1830918  read: 5272  total: 57344
size: 1830918  read: 8192  total: 65536
size: 1830918  read: 8192  total: 73728
size: 1830918  read: 1460  total: 75188
size: 1830918  read: 6732  total: 81920
size: 1830918  read: 1460  total: 83380
size: 1830918  read: 6732  total: 90112
size: 1830918  read: 1460  total: 91572
size: 1830918  read: 6732  total: 98304
size: 1830918  read: 8192  total: 106496
size: 1830918  read: 1460  total: 107956
size: 1830918  read: 1460  total: 109416
size: 1830918  read: 5272  total: 114688
size: 1830918  read: 1460  total: 116148
size: 1830918  read: 6732  total: 122880
size: 1830918  read: 8192  total: 131072
size: 1830918  read: 8192  total: 139264
size: 1830918  read: 8192  total: 147456
size: 1830918  read: 1460  total: 148916
size: 1830918  read: 6732  total: 155648
size: 1830918  read: 8192  total: 163840
size: 1830918  read: 8192  total: 172032
size: 1830918  read: 8192  total: 180224
size: 1830918  read: 1460  total: 181684
size: 1830918  read: 6732  total: 188416
size: 1830918  read: 8192  total: 196608
size: 1830918  read: 8192  total: 204800
size: 1830918  read: 1460  total: 206260
size: 1830918  read: 6732  total: 212992
size: 1830918  read: 8192  total: 221184
size: 1830918  read: 8192  total: 229376
size: 1830918  read: 8192  total: 237568
size: 1830918  read: 8192  total: 245760
size: 1830918  read: 1460  total: 247220
size: 1830918  read: 6732  total: 253952
size: 1830918  read: 1460  total: 255412
size: 1830918  read: 6732  total: 262144
size: 1830918  read: 8192  total: 270336
size: 1830918  read: 1460  total: 271796
size: 1830918  read: 6732  total: 278528
size: 1830918  read: 1460  total: 279988
size: 1830918  read: 6732  total: 286720
size: 1830918  read: 8192  total: 294912
size: 1830918  read: 1460  total: 296372
size: 1830918  read: 6732  total: 303104
size: 1830918  read: 16384  total: 319488
size: 1830918  read: 8192  total: 327680
size: 1830918  read: 1460  total: 329140
size: 1830918  read: 6732  total: 335872
size: 1830918  read: 8192  total: 344064
size: 1830918  read: 1460  total: 345524
size: 1830918  read: 6732  total: 352256
size: 1830918  read: 8192  total: 360448
size: 1830918  read: 8192  total: 368640
size: 1830918  read: 8192  total: 376832
size: 1830918  read: 8192  total: 385024
size: 1830918  read: 8192  total: 393216
size: 1830918  read: 8192  total: 401408
size: 1830918  read: 8192  total: 409600
size: 1830918  read: 1460  total: 411060
size: 1830918  read: 6732  total: 417792
size: 1830918  read: 8192  total: 425984
size: 1830918  read: 8192  total: 434176
size: 1830918  read: 8192  total: 442368
size: 1830918  read: 8192  total: 450560
size: 1830918  read: 8192  total: 458752
size: 1830918  read: 8192  total: 466944
size: 1830918  read: 8192  total: 475136
size: 1830918  read: 1460  total: 476596
size: 1830918  read: 6732  total: 483328
size: 1830918  read: 1460  total: 484788
size: 1830918  read: 6732  total: 491520
size: 1830918  read: 1460  total: 492980
size: 1830918  read: 6732  total: 499712
size: 1830918  read: 8192  total: 507904
size: 1830918  read: 8192  total: 516096
size: 1830918  read: 8192  total: 524288
size: 1830918  read: 8192  total: 532480
size: 1830918  read: 8192  total: 540672
size: 1830918  read: 8192  total: 548864
size: 1830918  read: 1460  total: 550324
size: 1830918  read: 6732  total: 557056
size: 1830918  read: 1460  total: 558516
size: 1830918  read: 6732  total: 565248
size: 1830918  read: 1460  total: 566708
size: 1830918  read: 6732  total: 573440
size: 1830918  read: 8192  total: 581632
size: 1830918  read: 8192  total: 589824
size: 1830918  read: 8192  total: 598016
size: 1830918  read: 8192  total: 606208
size: 1830918  read: 8192  total: 614400
size: 1830918  read: 1460  total: 615860
size: 1830918  read: 6732  total: 622592
size: 1830918  read: 8192  total: 630784
size: 1830918  read: 8192  total: 638976
size: 1830918  read: 8192  total: 647168
size: 1830918  read: 8192  total: 655360
size: 1830918  read: 8192  total: 663552
size: 1830918  read: 8192  total: 671744
size: 1830918  read: 1460  total: 673204
size: 1830918  read: 6732  total: 679936
size: 1830918  read: 1460  total: 681396
size: 1830918  read: 1460  total: 682856
size: 1830918  read: 1460  total: 684316
size: 1830918  read: 3812  total: 688128
size: 1830918  read: 8192  total: 696320
size: 1830918  read: 1460  total: 697780
size: 1830918  read: 6732  total: 704512
size: 1830918  read: 8192  total: 712704
size: 1830918  read: 8192  total: 720896
size: 1830918  read: 8192  total: 729088
size: 1830918  read: 8192  total: 737280
size: 1830918  read: 8192  total: 745472
size: 1830918  read: 8192  total: 753664
size: 1830918  read: 8192  total: 761856
size: 1830918  read: 8192  total: 770048
size: 1830918  read: 8192  total: 778240
size: 1830918  read: 8192  total: 786432
size: 1830918  read: 8192  total: 794624
size: 1830918  read: 8192  total: 802816
size: 1830918  read: 1460  total: 804276
size: 1830918  read: 6732  total: 811008
size: 1830918  read: 1460  total: 812468
size: 1830918  read: 6732  total: 819200
size: 1830918  read: 8192  total: 827392
size: 1830918  read: 1460  total: 828852
size: 1830918  read: 6732  total: 835584
size: 1830918  read: 8192  total: 843776
size: 1830918  read: 1460  total: 845236
size: 1830918  read: 1460  total: 846696
size: 1830918  read: 5272  total: 851968
size: 1830918  read: 8192  total: 860160
size: 1830918  read: 8192  total: 868352
size: 1830918  read: 8192  total: 876544
size: 1830918  read: 1460  total: 878004
size: 1830918  read: 6732  total: 884736
size: 1830918  read: 8192  total: 892928
size: 1830918  read: 1460  total: 894388
size: 1830918  read: 6732  total: 901120
size: 1830918  read: 4380  total: 905500
size: 1830918  read: 1460  total: 906960
size: 1830918  read: 2352  total: 909312
size: 1830918  read: 8192  total: 917504
size: 1830918  read: 8192  total: 925696
size: 1830918  read: 1460  total: 927156
size: 1830918  read: 6732  total: 933888
size: 1830918  read: 1460  total: 935348
size: 1830918  read: 6732  total: 942080
size: 1830918  read: 8192  total: 950272
size: 1830918  read: 8192  total: 958464
size: 1830918  read: 8192  total: 966656
size: 1830918  read: 8192  total: 974848
size: 1830918  read: 8192  total: 983040
size: 1830918  read: 1460  total: 984500
size: 1830918  read: 6732  total: 991232
size: 1830918  read: 8192  total: 999424
size: 1830918  read: 1460  total: 1000884
size: 1830918  read: 1460  total: 1002344
size: 1830918  read: 5272  total: 1007616
size: 1830918  read: 8192  total: 1015808
size: 1830918  read: 8192  total: 1024000
size: 1830918  read: 8192  total: 1032192
size: 1830918  read: 8192  total: 1040384
size: 1830918  read: 8192  total: 1048576
size: 1830918  read: 8192  total: 1056768
size: 1830918  read: 8192  total: 1064960
size: 1830918  read: 8192  total: 1073152
size: 1830918  read: 8192  total: 1081344
size: 1830918  read: 1460  total: 1082804
size: 1830918  read: 1460  total: 1084264
size: 1830918  read: 5272  total: 1089536
size: 1830918  read: 1460  total: 1090996
size: 1830918  read: 1460  total: 1092456
size: 1830918  read: 5272  total: 1097728
size: 1830918  read: 8192  total: 1105920
size: 1830918  read: 8192  total: 1114112
size: 1830918  read: 1460  total: 1115572
size: 1830918  read: 6732  total: 1122304
size: 1830918  read: 8192  total: 1130496
size: 1830918  read: 1460  total: 1131956
size: 1830918  read: 1460  total: 1133416
size: 1830918  read: 5272  total: 1138688
size: 1830918  read: 8192  total: 1146880
size: 1830918  read: 8192  total: 1155072
size: 1830918  read: 1460  total: 1156532
size: 1830918  read: 1460  total: 1157992
size: 1830918  read: 5272  total: 1163264
size: 1830918  read: 8192  total: 1171456
size: 1830918  read: 8192  total: 1179648
size: 1830918  read: 8192  total: 1187840
size: 1830918  read: 8192  total: 1196032
size: 1830918  read: 1460  total: 1197492
size: 1830918  read: 6732  total: 1204224
size: 1830918  read: 1460  total: 1205684
size: 1830918  read: 1460  total: 1207144
size: 1830918  read: 5272  total: 1212416
size: 1830918  read: 8192  total: 1220608
size: 1830918  read: 8192  total: 1228800
size: 1830918  read: 8192  total: 1236992
size: 1830918  read: 4380  total: 1241372
size: 1830918  read: 3812  total: 1245184
size: 1830918  read: 8192  total: 1253376
size: 1830918  read: 1460  total: 1254836
size: 1830918  read: 6732  total: 1261568
size: 1830918  read: 8192  total: 1269760
size: 1830918  read: 8192  total: 1277952
size: 1830918  read: 8192  total: 1286144
size: 1830918  read: 1460  total: 1287604
size: 1830918  read: 6732  total: 1294336
size: 1830918  read: 8192  total: 1302528
size: 1830918  read: 1460  total: 1303988
size: 1830918  read: 6732  total: 1310720
size: 1830918  read: 1460  total: 1312180
size: 1830918  read: 6732  total: 1318912
size: 1830918  read: 8192  total: 1327104
size: 1830918  read: 8192  total: 1335296
size: 1830918  read: 1460  total: 1336756
size: 1830918  read: 6732  total: 1343488
size: 1830918  read: 1460  total: 1344948
size: 1830918  read: 6732  total: 1351680
size: 1830918  read: 8192  total: 1359872
size: 1830918  read: 8192  total: 1368064
size: 1830918  read: 8192  total: 1376256
size: 1830918  read: 8192  total: 1384448
size: 1830918  read: 8192  total: 1392640
size: 1830918  read: 8192  total: 1400832
size: 1830918  read: 8192  total: 1409024
size: 1830918  read: 8192  total: 1417216
size: 1830918  read: 8192  total: 1425408
size: 1830918  read: 8192  total: 1433600
size: 1830918  read: 1460  total: 1435060
size: 1830918  read: 1460  total: 1436520
size: 1830918  read: 5272  total: 1441792
size: 1830918  read: 1460  total: 1443252
size: 1830918  read: 1460  total: 1444712
size: 1830918  read: 5272  total: 1449984
size: 1830918  read: 8192  total: 1458176
size: 1830918  read: 8192  total: 1466368
size: 1830918  read: 1460  total: 1467828
size: 1830918  read: 6732  total: 1474560
size: 1830918  read: 8192  total: 1482752
size: 1830918  read: 1460  total: 1484212
size: 1830918  read: 6732  total: 1490944
size: 1830918  read: 8192  total: 1499136
size: 1830918  read: 1460  total: 1500596
size: 1830918  read: 6732  total: 1507328
size: 1830918  read: 1460  total: 1508788
size: 1830918  read: 6732  total: 1515520
size: 1830918  read: 1460  total: 1516980
size: 1830918  read: 1460  total: 1518440
size: 1830918  read: 5272  total: 1523712
size: 1830918  read: 8192  total: 1531904
size: 1830918  read: 1460  total: 1533364
size: 1830918  read: 6732  total: 1540096
size: 1830918  read: 8192  total: 1548288
size: 1830918  read: 5840  total: 1554128
size: 1830918  read: 2352  total: 1556480
size: 1830918  read: 8192  total: 1564672
size: 1830918  read: 8192  total: 1572864
size: 1830918  read: 8192  total: 1581056
size: 1830918  read: 8192  total: 1589248
size: 1830918  read: 8192  total: 1597440
size: 1830918  read: 1460  total: 1598900
size: 1830918  read: 6732  total: 1605632
size: 1830918  read: 8192  total: 1613824
size: 1830918  read: 8192  total: 1622016
size: 1830918  read: 8192  total: 1630208
size: 1830918  read: 8192  total: 1638400
size: 1830918  read: 1460  total: 1639860
size: 1830918  read: 1460  total: 1641320
size: 1830918  read: 5272  total: 1646592
size: 1830918  read: 8192  total: 1654784
size: 1830918  read: 1460  total: 1656244
size: 1830918  read: 6732  total: 1662976
size: 1830918  read: 8192  total: 1671168
size: 1830918  read: 8192  total: 1679360
size: 1830918  read: 1460  total: 1680820
size: 1830918  read: 6732  total: 1687552
size: 1830918  read: 8192  total: 1695744
size: 1830918  read: 1460  total: 1697204
size: 1830918  read: 6732  total: 1703936
size: 1830918  read: 8192  total: 1712128
size: 1830918  read: 8192  total: 1720320
size: 1830918  read: 8192  total: 1728512
size: 1830918  read: 8192  total: 1736704
size: 1830918  read: 1460  total: 1738164
size: 1830918  read: 6732  total: 1744896
size: 1830918  read: 1460  total: 1746356
size: 1830918  read: 1460  total: 1747816
size: 1830918  read: 5272  total: 1753088
size: 1830918  read: 8192  total: 1761280
size: 1830918  read: 8192  total: 1769472
size: 1830918  read: 1460  total: 1770932
size: 1830918  read: 1460  total: 1772392
size: 1830918  read: 5272  total: 1777664
size: 1830918  read: 1460  total: 1779124
size: 1830918  read: 1460  total: 1780584
size: 1830918  read: 5272  total: 1785856
size: 1830918  read: 8192  total: 1794048
size: 1830918  read: 8192  total: 1802240
size: 1830918  read: 8192  total: 1810432
size: 1830918  read: 8192  total: 1818624
size: 1830918  read: 1460  total: 1820084
size: 1830918  read: 6732  total: 1826816
size: 1830918  read: 1460  total: 1828276
size: 1830918  read: 1460  total: 1829736
size: 1830918  read: 5272  total: 1835008
  

在客户端,它说文件已发送。但服务器端仍在等待   当我做比较 filesize == totalRead 问题是,   为什么服务器接收的数量超过我发送的数量?真的不是那个   要接收的字节数相同吗?

奇怪的是,当我终止应用程序时,写入的文件约为16MB。我如何处理接收部分肯定有问题。我需要一些帮助。

修改的 关于将16MB写入输出,我只是想通了。无论我读多少,我都在写整个缓冲区。所以我修改了上面的行

outputStream.write(readBuffer);
像这样

outputStream.write(readBuffer, 0, read);

我的问题仍然存在。我怎么没有收到字节数?我收到的不仅仅是我送的。任何线索?

1 个答案:

答案 0 :(得分:2)

您的副本循环都是错误的。发送循环应如下所示:

while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

否则你至少在文件的末尾写垃圾,也可能在其他时候写垃圾。

在读取完正确的字节数后,你的接收循环需要停止,所以它有点复杂:

while (total < length && (count = in.read(buffer, 0, (int)Math.min(buffer.length, length-total))) > 0)
{
    out.write(buffer, 0, count);
    total += count;
}

E&安培; OE