0

I'm working in a remote administration tool to my own use and the project is stopped because a error on receiver function on Server side that is this.:

Arithmetic operation resulted in an overflow. (Adding integers)

in this line:

var imageData = new byte[length];

enter image description here

void server_Received(Listener l, Info i, string received) {

    string[] cmd = received.Split('|');

    switch (cmd[1])
    {

        case "PRINT":

            //MessageBox.Show("print");

            Thread threadPrint;

            threadPrint = new Thread(() =>
            {
                var lengthData = new byte[4];
                var lengthBytesRead = 0;

                while (lengthBytesRead < lengthData.Length)
                {
                    var read = i.sock.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);
                    if (read == 0) return;
                    lengthBytesRead += read;
                }
                var length = BitConverter.ToInt32(lengthData, 0);
                var imageData = new byte[length];
                var imageBytesRead = 0;

                while (imageBytesRead < imageData.Length)
                {
                    var read = i.sock.Receive(imageData, imageBytesRead, imageData.Length - imageBytesRead, SocketFlags.None);
                    if (read == 0) return;
                    imageBytesRead += read;
                }

                using (var stream = new MemoryStream(imageData))
                {
                    var bitmap = new Bitmap(stream);
                    Invoke(new ImageCompleteDelegate(ImageComplete), new object[] { bitmap });
                }

            });

            threadPrint.Start();

            break;
    }
}

the code to receiver i taked from this reference (see Client code) and was adapted to my need like you can see above.

Here is how i'm sending the bitmap on android (Java):

try {
                dos = new DataOutputStream(SocketBackgroundService.xclientSocket.getOutputStream());
                PrintWriter out = new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(dos)
                        )
                );
                out.println("|PRINT|");
                out.flush();
                dos.writeInt(array.length);
                dos.write(array, 0, array.length);
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

Then, i want know what can be done to solve this trouble?

Thanks in advance.

  • You can't create an array with a negative length (which appears to be the exception you're getting). Therefore, the way you're getting your length and converting it to an int is the issue. You're getting negatives due to the size of the image you've passed in. I'd first suggest trying it with smaller bitmaps, see if it works with something you gurantee to have a length less than 2^32. I don't know how to fix this without using more arrays. Basically a list of byte arrays, each of which holds the image data, then you update one bitmap with one memory stream made up of list of image data. – DubDub Oct 22 '18 at 13:00
  • @DubDub, thank you by your suggestion, but the error come with any size of image (bitmap). –  Oct 22 '18 at 13:14
  • No problem, this Info class you refer to as a parameter, can you edit it in please? Or provide a link to the documentation for it. I'm curious about how you set up the socket. – DubDub Oct 22 '18 at 13:20
  • @DubDub, You can find on this my other recent [question](https://stackoverflow.com/q/52910928/9672569) –  Oct 22 '18 at 13:27
  • This is a pretty tough one, the problem is with your line 'var read = i.sock.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);' This is returning the wrong length into lengthData, the first byte shouldn't be negative. – DubDub Oct 22 '18 at 14:18
  • The other issue could be that the data being received is in the wrong order, hence you might was to change 'lengthData.Length - lengthBytesRead' to 'lengthBytesRead' in your parameters for the line I mentioned above. – DubDub Oct 22 '18 at 14:28
  • Another issue could be you've set up the socket wrong, and there are a whole plethora of issues that could arise there. – DubDub Oct 22 '18 at 14:29
  • @DubDub, ok. Then this C# algorithm to receive a bitmap from Java android is right (the step by step), is need only some adjusts, ok? –  Oct 22 '18 at 14:56
  • You can test the algorithm yourself if you wish by getting a file in code, and using it's properties such as it's size to know how big it is, the get the bytes for that file and create a memory stream like you've done. I won't test the code for you, however it looks good to me, I think the algorithm is correct, just as you say, needs some adjustments. – DubDub Oct 22 '18 at 15:05
  • 1
    @DubDub, i tested your suggestion `"change 'lengthData.Length - lengthBytesRead' to 'lengthBytesRead' "`, the exception above was solved, but still not works (not able to receive the bitmap). Even so, thank you. –  Oct 22 '18 at 17:05

1 Answers1

3

The solution was insert:

Array.Reverse(lengthData);

before of:

var length = BitConverter.ToInt32(lengthData, 0);