After sending byte arrays via dataoutputstream, the received byte arrays are not equal to original byte arrays

拈花ヽ惹草 提交于 2019-12-12 04:46:46

问题


For some reasons, I need to send multiple byte arrays separately via server socket, and client socket will receive these byte arrays. After sending byte arrays, I found byte arrays received in client socket are not equal to those in server socket. If I use ObjectOutputStream and ObjectInputStream, then everything works fine, but for my need, I can't use ObjectOutputStream & ObjectInputStream, because my server need to connect more than 2 sockets.

Here is Server and Client code:

Server(Sender):

    private void sendData() throws IOException {
        DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

        File file = new File(filePath);
        byte[] fileByte = new byte[(int) file.length()];

        // Convert file into byte array
        FileInputStream fileInputStream = new FileInputStream(file);
        fileInputStream.read(fileByte);

        while (!decoder.isDecoded()) {
            byte[] packet = new byte[encoder.getPacketLength()];
            encoder.getPacket(packet, 0);

            if (Math.random() < 0.1)
                continue;

            // Send data HERE!!!
            outputStream.writeInt(encoder.getPacketLength());
            outputStream.write(packet);

            StringBuilder sb = new StringBuilder();
            for (byte value : packet) {
                sb.append(value);
            }
            serverLog("Packet content= " + sb.toString());

            decoder.addPacket(packet, 0);
        }

        outputStream.close();
        outputStream.flush();
        fileInputStream.close();
    }

Client(Receiver):

public void receiveData() throws UnknownHostException, IOException, ClassNotFoundException {
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    int len = dis.readInt();

    while (!decoder.isDecoded()) {
        byte[] packet = new byte[len];

        dis.read(packet, 0, len);

        StringBuilder sb = new StringBuilder();
        for (byte value : packet) {
            sb.append(value);
        }
        messageArea.append("Packet content= " + sb.toString() + "\n");

        decoder.addPacket(packet, 0);
    }
}

log from server:

Packet content= 8312654-7646-55-71-522462106-89115-98-107-66-9445-61-78-99-43-52528422-10765-3719-1243-114-115-5869-119-92-113591131276057-905042126-7557940-2850118-9737-6512759-93-393-89-96104-789-7694747-118-61-79-97-872-4369-82-11923-805-62-9527-38-1195-13107673425588-92-49-44-97-7811812416-781244-100
Packet content= -10295-4-73-809789-87-97-9095-55-11-7440100-2871-89-68-59-3739-2073-1286290-21-106-2-25-1263646-107-84-9-42-4491-418419-469395-61-52-32-85-38-107-8385-6559-1286575-56-1029-5991-7551-10390-25-94-5072-2318-62-12360-818-11719-73710076121-584012212645-20-170-45089-87401082110113-7-1034873-877
Packet content= -37-732-59-1071015223-34-2745-4322265-18-108-97126-22-56083-131-61-16069536-10-7092-36-50-96-72-3133-79-11751-3671-91-48-14-54-113-8295-124-18-78125-117-12811911042-34108-94-33-92-113268272-41-23992553-69-72997619-1166735417-76-126-58121-7595-443265-944-44-124104-201040-803310126-125-8449-8
Packet content= -102110606194-6037-2-63-6025-112-17-36-3629-93112-86-116-54-14118-124121315341-11923-119-12246-1-973-31-22-9-111-12614-37-1456-10456-9450-11823811378-779-111101-31-101-843217-124-16107-108107101-7743-108-100-5475113119-6647-7185-96-683-12710756-9896111-1072318-3982-6288-7496-5514-18373957-71108-3232
Packet content= -6689-124263-68321112-19-6730-68-12223-125105-51444110862-11063-114-41-76-5275-7-15105-865826-5049-12386124123-121-10460-863108-65-481142910461-97-11912453-10718-6711787-91-37-97910-22-1963-88-116114-138375-2082027-128126-8529-3632242-772-105106-35-41-44-573126-7512038-62-733838-434433-9-59
Packet content= -9652035246-1482-59-63699991-47-25-2-15127-55-42-97-106-11966-15-4344462-68-2-916794121-31-11164-40-73-8104342-69-38-122986-11584-57-106-35-49-120-15138-19121-32-88-62-5127-95114-26-34-75-377155-6911914-4210849-116-7028-8180-817585-108-76-3337-21-4589108-121794-85109-6810644-102101106-68-5-127
Packet content= -127-124-61-24479910112178-45-47-952540-122-70100-120-17-5114-127-69-12-38-100-111-21-111-44113-10-12490-72884-9549-1115580-7653-16100-7889-22103-747-476698-9-68-83-301112667-1712127-64-45-746-1054497-7071108-109-9-8946-4259-121-106-121-27-6761-3447-1177476-7271-95-75-107-42-331886-119-10578-6098-128-2287-52
Packet content= 5244-100-68-1215109-27-6810-774491-11-29102-6267-621964-975427701107495-33578037-1924-5611657210-46-16-94-53-92-5282-11353-10359-38-1-385212480-12710119-597634-108-67-49109101-69-7933-10189120-76104-46106-64223-44-10-4789-1213910192-125-60-5-20-37-901-5-104103-739314-4414-1123759-31-45-72
Packet content= 119-4161558-7832-8310329-118-91122-85-37-33-30-52-101087-123784011115-57-31-1166-31-55-43-13-49-5812464-16-1382-30-3810445117659-70810584-46113-11-5111-1251-428299112-816596-1091021353-4679-3526-10363100-10-4-318-49-117-10449103105-11190-94-5310633-2-123-5268-119124-12297-106-114-102110-35-12094-128
Packet content= -381-70-109-26112-2552-92-40-39-22966-36-69-118-116-19-4848-9-12869-102-4558-2198362-121-553776-84-118-93125-12374128555-8688-2-729-43126-3-10083-26-64119-6010577-12263-4-74-44-11028-216447727-5082-104100-3410673529-8970-27110-81-90-1221185-22-249510515-123-157817-43-11961-93141-8512-77-7352

log from client:

Packet content= 8312654-7646-55-71-522462106-89115-98-107-66-9445-61-78-99-43-52528422-10765-3719-1243-114-115-5869-119-92-113591131276057-905042126-7557940-2850118-9737-6512759-93-393-89-96104-789-7694747-118-61-79-97-872-4369-82-11923-805-62-9527-38-1195-13107673425588-92-49-44-97-7811812416-781244-100
Packet content= 000110-10295-4-73-809789-87-97-9095-55-11-7440100-2871-89-68-59-3739-2073-1286290-21-106-2-25-1263646-107-84-9-42-4491-418419-469395-61-52-32-85-38-107-8385-6559-1286575-56-1029-5991-7551-10390-25-94-5072-2318-62-12360-818-11719-73710076121-584012212645-20-170-45089-87401082110113-7-103
Packet content= 4873-877000110-37-732-59-1071015223-34-2745-4322265-18-108-97126-22-56083-131-61-16069536-10-7092-36-50-96-72-3133-79-11751-3671-91-48-14-54-113-8295-124-18-78125-117-12811911042-34108-94-33-92-113268272-41-23992553-69-72997619-1166735417-76-126-58121-7595-443265-944-44-124104-201040
Packet content= -803310126-125-8449-8000110-102110606194-6037-2-63-6025-112-17-36-3629-93112-86-116-54-14118-124121315341-11923-119-12246-1-973-31-22-9-111-12614-37-1456-10456-9450-11823811378-779-111101-31-101-843217-124-16107-108107101-7743-108-100-5475113119-6647-7185-96-683-12710756-9896111-1072318-3982-6288
Packet content= -7496-5514-18373957-71108-3232000110-6689-124263-68321112-19-6730-68-12223-125105-51444110862-11063-114-41-76-5275-7-15105-865826-5049-12386124123-121-10460-863108-65-481142910461-97-11912453-10718-6711787-91-37-97910-22-1963-88-116114-138375-2082027-128126-8529-3632242-772-105106-35-41
Packet content= -44-573126-7512038-62-733838-434433-9-59000110-9652035246-1482-59-63699991-47-25-2-15127-55-42-97-106-11966-15-4344462-68-2-916794121-31-11164-40-73-8104342-69-38-122986-11584-57-106-35-49-120-15138-19121-32-88-62-5127-95114-26-34-75-377155-6911914-4210849-116-7028-8180-817585-108-76
Packet content= -3337-21-4589108-121794-85109-6810644-102101106-68-5-127000110-127-124-61-24479910112178-45-47-952540-122-70100-120-17-5114-127-69-12-38-100-111-21-111-44113-10-12490-72884-9549-1115580-7653-16100-7889-22103-747-476698-9-68-83-301112667-1712127-64-45-746-1054497-7071108-109-9-8946-4259-121-106-121-27-67
Packet content= 61-3447-1177476-7271-95-75-107-42-331886-119-10578-6098-128-2287-520001105244-100-68-1215109-27-6810-774491-11-29102-6267-621964-975427701107495-33578037-1924-5611657210-46-16-94-53-92-5282-11353-10359-38-1-385212480-12710119-597634-108-67-49109101-69-7933-10189120-76104-46106-64223-44
Packet content= -10-4789-1213910192-125-60-5-20-37-901-5-104103-739314-4414-1123759-31-45-72000110119-4161558-7832-8310329-118-91122-85-37-33-30-52-101087-123784011115-57-31-1166-31-55-43-13-49-5812464-16-1382-30-3810445117659-70810584-46113-11-5111-1251-428299112-816596-1091021353-4679-3526-10363100
Packet content= -10-4-318-49-117-10449103105-11190-94-5310633-2-123-5268-119124-12297-106-114-102110-35-12094-128000110-381-70-109-26112-2552-92-40-39-22966-36-69-118-116-19-4848-9-12869-102-4558-2198362-121-553776-84-118-93125-12374128555-8688-2-729-43126-3-10083-26-64119-6010577-12263-4-74-44-11028-216447727-5082

Everyone can see there are always 6 integers "000110" inserted into the received packets after the first packet, how do I edit these code to prevent this problem and ensure the sent packets and received packets are equal? Please help me, I would appreciate your kind help. :)


回答1:


You're assuming that read() filled the buffer, without checking the return value. It isn't specified to do that, only to transfer at least one byte. Use readFully() to get that behaviour.



来源:https://stackoverflow.com/questions/23761984/after-sending-byte-arrays-via-dataoutputstream-the-received-byte-arrays-are-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!