Sending Files with TCP/IP

Swissmade

Well-Known Member
Licensed User
Longtime User
Hello all,
I like to send a image taken with the Camera to a external Server.
I have the Connection to the Server waiting for Data.
The problem is how to send the data with a filename to the Server.

Please Help:sign0085::sign0085:

Thanks
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Assuming that you are writing the server code as well:
- Convert the file name to bytes with String.GetBytes. (FileName.GetBytes("UTF8"))
- Send the length of this array.
- Send the array (OutputStream.WriteBytes)
- Send the length of the bitmap array.
- Convert the bytes array to InputStream with InputStream.InitializeFromBytesArray.
- Use File.Copy2 to copy the InputStream to the network OutputStream.

Your server code should of course adhere to the same protocol.
Tips: do not use c# ReadString in your server code. It expects a specific .Net protocol.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
using Android

Assuming that you are writing the server code as well:
- Convert the file name to bytes with String.GetBytes. (FileName.GetBytes("UTF8"))
- Send the length of this array.
- Send the array (OutputStream.WriteBytes)
- Send the length of the bitmap array.
- Convert the bytes array to InputStream with InputStream.InitializeFromBytesArray.
- Use File.Copy2 to copy the InputStream to the network OutputStream.

Your server code should of course adhere to the same protocol.
Tips: do not use c# ReadString in your server code. It expects a specific .Net protocol.

Thanks Erel I go to try.

:sign0013: Erel I dont get it completly is there a Code Sample of this.

Hi Erel I found it out many thanks for your Hints.
 
Last edited:
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Code To Send Files with TCP/IP

Hi Erel,

The Code to Send Files
B4X:
Sub SendData
Dim In As InputStream 
Dim AddString As String
Dim Buffer() As Byte 
Dim Now As Long
Now = DateTime.Now  

'Send the Stream to the Server after Connection
    IN.InitializeFromBytesArray(OutStream, 0, OutStream.Length)
    File.Copy2(IN,socket1.OutputStream)
'Add File name to Image end
'Get Filename Byte Array place is Buffer
    DateTime.DateFormat = "yyyymmdd"
    AddString = CRLF & "FILENAME=" & DateTime.Date(Now) & "_" & DateTime.Time(Now)
    AddString = AddString & ".jpg"
    Buffer = AddString.GetBytes("UTF8")
'Put 'Filename to the end of the Images Array
    IN.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    File.Copy2(IN,socket1.OutputStream)
    
    Socket1.Close
End Sub
Hope it helps somebody:):sign0060:
 
Last edited:
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Did you ever get FILE transfer to work from PC to Android? If so, can you provide sample code on both ends?
Thanks,
Rusty
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Sending Files from Android to PC

Did you ever get FILE transfer to work from PC to Android? If so, can you provide sample code on both ends?
Thanks,
Rusty

To do this you need an IP Listener what is receiving the file.
Listener can also be made on Android but only for local Network.


Cheers,
Swissmade.
 
Last edited:
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
I have a VB 2010 application that is currently communicating short messages and is receiving SQL data to write on the PC based MS/SQL server database. These all work well, but when I try to get my PC system to send a file (about 1MB), i often get parsing errors.
The reason the parsing errors occur is because I'm handling each "buffer" coming from the PC individually.
The Android max TCP buffer is apparently 1536 bytes and this is very slow, so I'm transmitting 64K bytes from the PC, receiving the 1535 byte "pieces" of the 64K buffer; assembling them in memory and then using them. Sometimes this gets out of sequence (don't know why), I'm debugging this and your post made me think that there might be a simple way to transmit the file using another method.
Thanks for your quick response,
Rusty
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Currently, I send the filename and size.
I then begin sending all the data in blocks of 64K
The android receives in 1.5K hunks, assembles the pieces and writes it to a file. Half the time it works perfectly the other half, it fails.
I'm using Astreams on the android side
and Sockets within VB.
Any suggestions are welcome.
Rusty
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Sending From PC To Droid or the other way arraound

Currently, I send the filename and size.
I then begin sending all the data in blocks of 64K
The android receives in 1.5K hunks, assembles the pieces and writes it to a file. Half the time it works perfectly the other half, it fails.
I'm using Astreams on the android side
and Sockets within VB.
Any suggestions are welcome.
Rusty

If you send from Droid to PC you have to send the File in a loop with small junks.
This prevent a slow connection to hang.

Cheers,
Swissmade
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel,
How large are the buffers being transmitted to the Android from the B4a compiler?
My PC "transmitter" is currently sending 64K "chunks" to the tablet. Each 64K bytes has a length header and I've been trying to patch each block together.
Do you have any PC source for your data/file transmitter I could look at?
Regards,
Rusty
 
Upvote 0
Top