I need some support for the implementation of a file transfer via the TCPSocket…
On the client side, a file is read in, which is then to be stored in a directory on the server side.
At the moment I’m a little bit hung up on converting the file content back and forth.
The message to be transmitted (including protocol information) is put together in a Xojo.core.MutableMemoryBlock. I’ll be fine until then.
The string that I want to transfer with “write()” is generated from a memory block as shown below:
Public Function ConvertMBToString(dst As Xojo.Core.MemoryBlock) as String
Return(CType(dst.Data, MemoryBlock).StringValue(0, dst.Size))
On the server side, this CString also arrives wonderfully in the ReadAll() of the TCPSocket.
But what happens then?
How do I get the string returned by ReadAll() converted back into a (Mutable)MemoryBlock, so that I can disassemble the message again?
Is the workaround with the classic MemoryBlock the only way?
dim mbc as MemoryBlock = Input
dim mb as new Xojo.Core.MemoryBlock( mbc, mbc.Size )
Thank you very much for your support.
In the DataAvailable TCPSocket event, do something like this:
Dim AReply as String
AReply = DefineEncoding(me.ReadAll, Encodings.UTF8)
Dim mb as new Xojo.Core.MutableMemoryBlock(Xojo.Core.TextEncoding.UTF8.ConvertTextToData(AReply.ToText))
Or it can go into one long line…
Dim mb as new Xojo.Core.MutableMemoryBlock(Xojo.Core.TextEncoding.UTF8.ConvertTextToData(DefineEncoding(me.ReadAll, Encodings.UTF8).ToText))
I will presume that all of the data has filled the socket buffer and this is to be read all-at-once. The mb memoryblock will contain the received string data from the ReadAll command.
Let me know if this works for you.
Thank you for your quick answer.
I mostly transfer binary data. Is there no problem with UTF8 encoding?
Yes, there can be a problem with UTF8 encoded data, as this type has a limited range. Usually UTF16 is used when reading and writing 16 bit data. If the end of the data had only 8-bits, then add 8 trailing zeros so that 16-bits of data can be read.
New framework memory blocks are more challenging. Maybe try and get it working with the classic memory block first.