通过dataoutputstream发送字节数组后,接收到的字节数组不等于原始字节数组 [英] After sending byte arrays via dataoutputstream, the received byte arrays are not equal to original byte arrays

查看:141
本文介绍了通过dataoutputstream发送字节数组后,接收到的字节数组不等于原始字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我需要通过服务器套接字分别发送多个字节数组,而客户端套接字将接收这些字节数组。发送字节数组后,我发现客户端套接字中接收的字节数组与服务器套接字中的字节数组不相等。如果我使用ObjectOutputStream和ObjectInputStream,那么一切正常,但是出于我的需要,我不能使用ObjectOutputStream& ObjectInputStream,因为我的服务器需要连接两个以上的套接字。

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:

服务器(发件人):

    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();
    }

客户(接收方):

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

来自客户端的日志:

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

每个人都可以看到在第一个数据包之后始终在接收的数据包中插入6个整数 000110,如何编辑这些代码以防止此问题并确保发送的数据包和接收的数据包相等?请帮助我,谢谢您的帮助。 :)

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. :)

推荐答案

您假设 read()填充了缓冲区,不检查返回值。未指定这样做,仅传输至少一个字节。使用 readFully()来获取该行为。

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.

这篇关于通过dataoutputstream发送字节数组后,接收到的字节数组不等于原始字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆