0

I plan to send and receive file with a microcontroller. I wrote up a simple protocol for both sender and receiver, but I have some trouble reconstructing the file back. I send the data in a stream of raw binary. However, I have not found the location of fileinfo (name, ext, size, etc.) in the file itself. Where is the fileinfo stored in the file? How does the OS know all these information if it isn't store in the file? (for e.g. name, extension, size, etc.)

Trivial question: Should I attach this file information with the protocol header? or should I just append it onto the file binary data?

4
  • 1
    Have you heard of file systems? If not: read about it. No, file information is not part of the file. You need to add it yourself. Mar 25, 2014 at 14:36
  • Please post the code you are using to open and send the file Mar 25, 2014 at 14:36
  • BTW - are you trying to re-implement FTP? Mar 25, 2014 at 14:37
  • @ChrisBallard I just need a simple file protocol. Because microcontroller C# library doesn't let me read byte by byte (it's actually byte-bit or byte with only 0 and 1 value), so I have to write my own receiver protocol (Convert 8 byte-bit to byte, Read it, If this is header information then process it, etc.).
    – pbeta
    Mar 25, 2014 at 15:48

2 Answers 2

0

You need to attach that information to your binary data yourself. If you have a binary stream, I suggest (it's easiest) you provide a fixed size header that contains all the file meta information. Then you append the file's content.

Why fixed size? Well, otherwise the receiver doesn't know where the file's content starts. You could also provide the header size in the first X bytes of the stream and then have a variable sized header. As you like it, but I prefer the fixed size solution.

Example for fixed size header:

<255 bytes file name><8 bytes file size><Content...>

Example for dynamically sized header:

<4 bytes length of file name><x bytes file name><8 bytes file size><Content...>

Let me stress that it is very important that you also transmit the size of the content in bytes, so that the receiver knows how many bytes to read! Packets may be fragmented, you know?

0

How does your self-made "protocol" work?

It is quite uncommon for files to store their own size, it is a responsibility of the underlying file system to keep track of that (name including extension, size, permissions, modification time, ...).

You can put the size information in the header, or if you are sure that a certain sequence of bytes is never sent as payload, you can use this as a termination sequence to tell the receiver to stop receiving.

7
  • Which is hard (the termination sequence) to find, because you need to find a sequence of bytes that's not part of the binary content itself... That's why you should in any case send the length of the content in bytes before sending the bytes. Mar 25, 2014 at 14:42
  • I would rather say "whenever possible send the length of the content"
    – Jasper
    Mar 25, 2014 at 14:45
  • Can you tell me a case when it isn't possible? Mar 25, 2014 at 14:47
  • e.g. if the source itself is recording the data from a physical sensor... but that's perhaps not was OP meant by "send a file via microcontroller"
    – Jasper
    Mar 25, 2014 at 14:49
  • In addition: Can you tell me a byte sequence that's not part of any file so I can be sure that I never have partial transmissions? Mar 25, 2014 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.