3 Replies - 558 Views - Last Post: 17 July 2020 - 08:26 PM Rate Topic: -----

#1 KC10513   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 13-October 17

Random bytes added after every n bytes

Posted 08 June 2020 - 12:55 AM

Hi all

I am working on a project that spool file from a mobile device to my server App. the file transferred is either PDF or JPG but when the transfer of the file start for some reason i get random byte added into my read chunks.
My Receive routine is Big, thus i cannot paste the whole code here but i'll try and explain it.
I have a state object that contains a buffer[16384] and a FileStream to write the received data. on the receive routine i have the EndRecieve Method[url="https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.endreceive?view=netcore-3.1"] that returns the number of bytes read.
While investigation i noticed that string "8000\r\n" get added in my file after receiving 32768 bytes, which by the way is the buffersize x2.
So i need help understanding this behavior and if possible help me solve the problem.

This post has been edited by KC10513: 08 June 2020 - 12:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Random bytes added after every n bytes

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Random bytes added after every n bytes

Posted 08 June 2020 - 09:43 AM

Well, if your code was folly best practices, then it would be using dependency injection. That would mean that you could derive a custom TestingFileStream class where you can set breakpoints for when those bad data gets written. With the breakpoint set, you can then go up the call stack to see where it is coming from.

On the other hand, if you were following best practices, you wouldn't have methods that exceed 25-30 lines and you would be able to post you receive code. And you would also have unit tests which would help you narrow down if those bad bytes are coming from your code and if so, which component.

My suggestion at this point is to start setting breakpoints and try to narrow down the source.
Was This Post Helpful? 0
  • +
  • -

#3 KeyWiz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 454
  • Joined: 26-October 06

Re: Random bytes added after every n bytes

Posted 16 July 2020 - 03:48 PM

View PostKC10513, on 08 June 2020 - 01:55 AM, said:

Hi all

I am working on a project that spool file from a mobile device to my server App. the file transferred is either PDF or JPG but when the transfer of the file start for some reason i get random byte added into my read chunks.
My Receive routine is Big, thus i cannot paste the whole code here but i'll try and explain it.
I have a state object that contains a buffer[16384] and a FileStream to write the received data. on the receive routine i have the EndRecieve Method[url="https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.endreceive?view=netcore-3.1"] that returns the number of bytes read.
While investigation i noticed that string "8000\r\n" get added in my file after receiving 32768 bytes, which by the way is the buffersize x2.
So i need help understanding this behavior and if possible help me solve the problem.

You have obviously hit the upper limit of data transfer, 32767 BYTES. The moment you attempt to send one byte more than 32767, you get an error. Why? because starting with item 0, you have used 32768 bytes at byte ID 32767. the 8000 is likely an error code you could look up that will likely be data overrun or out of memory but the \r\n are escape codes for \r = return and \n = newline. Reduce the number of characters you are attempting to read in at one time, or create a larger buffer.

Always remember to stop one less than your upper limit, whether you are working in bits, bytes, Mbytes, TB, etc.
2     =  0-1     BIT
4     =  0-3     NIBble also Word
8     =  0-7     BYTe
16    =  0-15
32    =  0-31
64    =  0-63
128   =  0-127
256   =  0-255
512   =  0-511
1024  =  0-1023  KiloByte
2048  =  0-2047
4096  =  0-4095
8192  =  0-8191
16384 =  0-16383
32768 =  0-32767
...


These are all upper limits of memory addressing, and you should always be one less when working with these limits. Since you are doubling your buffer, from 16384, at which you would need to stop at value 16383, then you get the doubling of the full value but it still needs to be one less or 32767. So, it is not a cumulative limit, but the limit is there none the less. I hope this helps you understand WHY this was happening.

This post has been edited by KeyWiz: 16 July 2020 - 04:47 PM

Was This Post Helpful? 1
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Random bytes added after every n bytes

Posted 17 July 2020 - 08:26 PM

Our OP is receiving data not sending them.

But if the OP has his limits his code set to have only a 16KB buffer, why is he getting the issue when being sent 32KB? He is not doubling his buffer.

And even worse, then he hits the 32KB, the sender also sends an extra set of bytes that look like the string "8000\r\n".

Also further recall that we are in the C# forum. Buffer overruns will result in an index out of bounds exception. But that is not the results he is seeing. Instead he is seeing random data showing up.

I don't understand why you downvoted by suggestion to set breakpoints to try to debug what is happening, and better yet, use dependency injection to be able to inject a file stream class fully under the programmers control to better be able to place breakpoints and inspect the data flowing into the stream. What made that suggestion to actually debug the issue not useful?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1