Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / T1: Reading/Writing files byte by byte

Author
Message
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Oct 2012 17:42
I was hoping to send a file across the network by reading it in byte by byte, sending it as a string of perhaps 4K at at time, then writing it back to a file at the other side.

The problem is, there is no read by byte. I tried to be a bit creative but my logic is flawed:

1. Read file as integers (4 bytes)
2a. Reduce integer to 2 x 2-byte values and push them to a string using CHR()
2b. Reduce integer to 4 x 1-byte values and push them to a string using CHR()
3. Write string to file (as a test, omitting network for now)

Here's the code for the 4 x 1-byte attempt:



This doesn't take account of the final few bytes, but other than that I get a file of the same size (tested using a 630KB image file). However, the content of the file is completely corrupted.

Any ideas on how to achieve this challenge without a HTTP server?

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Oct 2012 17:45
Start of Original File:

‰PNG



Start of copied file:


IdBM±aYoD^œµd2’,™l™ó„2

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 5th Oct 2012 18:02
Don't bother converting, you are actually adding more than you need. And the WriteString is throwing in a null terminator, adding to the size of the file and probably corrupting it

You would be much better with this:


The above will run faster and not corrupt the file. The output will be in the same order as the input and it takes care of the entire file.

Cheers,
Ancient Lady
AGK Community Tester
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Oct 2012 18:16
Thanks Ancient Lady, that works a treat. I'll just need to check what happens to the very end of the file, where the final read may not be 4 bytes. I don't think I have an answer to this because I only have the option of 4-byte read/writes, or a null terminated string.

The other thing I would have liked to have is the ability to send across the network in bigger chunks rather than 4 bytes at a time, the write straight to a file is a non-networked test of the theory. But by your explanation this doesn't seem possible again due to the null terminator. At least I won't waste my time trying!

Hopefully I'll be reporting back with a success shortly

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 5th Oct 2012 18:26 Edited at: 5th Oct 2012 18:28
This works for me:


Edit: I'd guess you could send as much as you like from the array one way or another... maybe convert the integers into delimited strings or something?


this.mess = abs(sin(times#))
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Oct 2012 18:33
Thanks again for the confirmation. I think I've also solved the network component

msg = addNetworkMessage(msg)

followed by multiple

addNetworkMessageInteger(msg,intValue)

Then the message carries multiple values across the network in one collection.

I just need to sort the final bytes issue now

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Oct 2012 22:49
It's working
And it's fast 0.6 seconds for a 630KB file across my 54Mbs wireless network, including opening the file and breaking it down into packets of 1.4KB. This is a limit set by AppGameKit, and worth noting, otherwise the program crashes.

I'll try and post more technical details later, once I've completed the full thing. I still need to deal with those pesky last few bytes.

Thanks again for all the help and getting me back on track.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 6th Oct 2012 04:12
Glad to be of help. It sounds like a useful bit of functionality.

Happy Programming!

Cheers,
Ancient Lady
AGK Community Tester

Login to post a reply

Server time is: 2024-11-23 23:30:58
Your offset time is: 2024-11-23 23:30:58