Forum Archive

File transfer with multipeer

Drizzel

Hi,
I’ve made a multiplayer game some time ago using multipeer. Anyone who is interested can find some more information here.
Now I wondered if I could transfer files (preferably .m4a) over it. I modified the module to store the received data in the variable “received”, so that I could access it comfortably with “multipeer.received”.

I have tried reading files like this

with open(“file.m4a”) as f: 
    data = f.read()

but I always get an error that says cannot read byte at location...

I wanted to then transfer the string with multipeer to my iPad.
Is there any way to fix this, or could I send files from one device to another any other way?

cvp

@Drizzel Try

with open('sample.m4a',mode='rb') as f: 
Drizzel

@cvp I tried it out, I got a different error now. It's "Object of type 'bytes' is not JSON serializable". Any idea why?

cvp

@Drizzel I didn't know it was for use with jSON which is text only, I think

cvp

@Drizzel multipleer module doc says

Messages passed between peers are UTF-8 encoded text
cvp

@Drizzel you could encode your binary file as a string before sending and decode at reception, like

import base64

with open('sample.m4a',mode='rb') as f: 
    data = f.read()
data_str = str(base64.b64encode(data),'utf-8')
# send string


# receive string
data_out = base64.standard_b64decode(data_str)
with open('sample_out.m4a',mode='wb') as f: 
    f.write(data_out)
mikael

@Drizzel, the module these days has a streaming option as well. I have not really tried it but it might be more appropriate for large file transfer.

Drizzel

@mikael I had an old version installed, so I just updated. I will definitely look into it now. It’s interesting that it apparently doesn’t require strings, which might fix the errors I’m currently fighting with :)

@cvp I tried out your suggestion, and I now can transfer the data without getting errors! The issue I now have, is that the final file is about 2.5 times as large as the original, and it therefore won’t play. Reading in the file, converting it to a string and then writing it to a new file showed that the conversion to a string is where the data gets altered heavily, and obviously I have no idea what to do about it😂

Any ideas?

Drizzel

@mikael
I just gave it a first try, it didn’t completely work, but it was a promising start. The length of the data before sending and receiving is the same, so I’m assuming the data itself is fine. If I then write it to a file though, it’s only 719 bytes, not 4 mb as it should be. For now, I have no clue why, but as a I said it’s promising.

JonB

@Drizzel sounds like you are writing the file as text instead of binary, or are not decoding back to bytes?

cvp

@Drizzel Did you try my little script, output file is exactly the same as the input one.
As @JonB said, it seems you did forget one step.

Drizzel

@cvp @JonB @mikael I did forget to decode the data after receiving it, my bad. It works flawlessly now, thanks a lot!

mikael

@Drizzel, for future reference: did you messages or streaming?

Drizzel

@mikael messages, as it doesn’t actually take too long and I don’t have to manage many small bits of data, making it easier for me (with limited python knowledge) to transfer multiple files.