File Send Over Base64.

The place to discuss anything and everything about running your LiveCode on Android

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 3:23 am

Hii Guys!!

I have made some progress from my last post and I am losing packets during the file send using Base64.

My Client is:

Code: Select all

global WSITE

on openStack
   put "192.168.1.2:12345" into WSITE
   open socket to WSITE with message "DONEA"
end openStack

on DONEA theIP
   put "binfile:C:/Users/USER/Documents/TEST.jpg" into tFile
   put URL tFile into temp
   put base64encode(temp) into tAll
   replace cr with empty in tAll
   write tAll & "file§" to socket theIP
   read from socket theIP until "§" with message "MESSAGES1"
end DONEA

on MESSAGES1 theIP theMessage
   read from socket theIP until "§" with message "MESSAGES1"
end MESSAGES1
And my Server is:

Code: Select all

on openStack
   accept connections on port 12345 with message "CONNECT"
end openStack

on CONNECT theIP 
   read from socket theIP with message "NEWMESSAGE"
end CONNECT

on NEWMESSAGE theIP pmsg
   if char -5 to -1 of pmsg is "file§" then
      delete  char -5 to -1 of pmsg
      //replace cr with empty in pmsg
      put base64decode(pmsg) into temp
      put "C:/USERS/Goodie/Documents/TESTRECEIVE.jpg" into tName
      put temp into URL ("binfile:" & tName) 
   end if
   read from socket theIP until "§" with message "NEWMESSAGE"
end NEWMESSAGE
The majority of the image file is sent, although it is losing some packets over the transfer. Can anyone spot what is going on?

Many Thanks,

Googie.

Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

Re: File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 3:50 am

I am using this for an Android device, but I guess it falls under Windows development. Please move to an appropriate forum if inclined.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: File Send Over Base64.

Post by FourthWorld » Mon Aug 19, 2024 3:59 am

Googie85 wrote:
Mon Aug 19, 2024 3:23 am
The majority of the image file is sent, although it is losing some packets over the transfer.
How many bytes are missing and what's in those bytes?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

Re: File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 4:05 am

I am missing 2 bytes.

Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

Re: File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 4:16 am

When I open the received file in Notepad, it displays in Chinese.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: File Send Over Base64.

Post by FourthWorld » Mon Aug 19, 2024 4:32 am

Instead of reading to a specified character, have you tried reading for the length of the data?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

Re: File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 4:51 am

The number of chars sent is 718149
The number of chars received is 716689

A difference of 1460
Last edited by Googie85 on Mon Aug 19, 2024 5:40 am, edited 2 times in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: File Send Over Base64.

Post by FourthWorld » Mon Aug 19, 2024 5:36 am

What is the number of bytes in each?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

Re: File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 5:43 am

When I use, put the number of bytes in var, I get the same.

Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

Re: File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 5:55 am

The following code = 531232

Code: Select all

   put "binfile:C:/Users/USER/Documents/Screen.jpg" into tFile
   
   put URL tFile into temp
   
   put the number of bytes in temp into lopi
   answer lopi
The following code = 522592

Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

Re: File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 5:57 am

The following code = 531232

Code: Select all

   put "binfile:C:/Users/USER/Documents/Screen.jpg" into tFile
   put URL tFile into temp
   put the number of bytes in temp into lopi
   answer lopi
The following code = 522592

Code: Select all

   put "binfile:C:/Users/USER/Documents/ScreenReceive.jpg" into tFile
   put URL tFile into temp
   put the number of bytes in temp into lopi
   answer lopi

AndyP
Posts: 634
Joined: Wed Aug 27, 2008 12:57 pm
Contact:

Re: File Send Over Base64.

Post by AndyP » Mon Aug 19, 2024 8:06 am

Have a look at this post

viewtopic.php?t=13424 and see if it helps.
Andy .... LC CLASSIC ROCKS!

Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

Re: File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 8:20 am

Thanks heaps for your reply AndyP!!! Unfortunantely I cannot find a workable solution from it.

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1232
Joined: Thu Apr 11, 2013 11:27 am

Re: File Send Over Base64.

Post by LCMark » Mon Aug 19, 2024 9:36 am

@Googie85: I think the issue lies in your server-side code:

On getting a connection, the server is doing this:

Code: Select all

on CONNECT theIP 
   read from socket theIP with message "NEWMESSAGE"
end CONNECT
Here, the 'read' does not have any length or boundary specified so it will send NEWMESSAGE as soon as *any data* is received. As data is broken up and sent over sockets in small packets, how much data you are getting will vary considerably depending on time/network speed.

Then, on receiving data, the server is doing this:

Code: Select all

on NEWMESSAGE theIP pmsg
   if char -5 to -1 of pmsg is "file§" then
      delete  char -5 to -1 of pmsg
      //replace cr with empty in pmsg
      put base64decode(pmsg) into temp
      put "C:/USERS/Goodie/Documents/TESTRECEIVE.jpg" into tName
      put temp into URL ("binfile:" & tName) 
   end if
   read from socket theIP until "§" with message "NEWMESSAGE"
end NEWMESSAGE
Here, the first block of data you get (as a result of the read in CONNECT) will likely not be complete, so it will be ignored - you aren't saving 'pmsg' so its just lost.

Then, when you do a read *with* a boundary condition (until ...) it will then not get sent again until the rest of the data is there.

I think it should be fixed by making the initial 'read' (in CONNECT) 'read until "§"'. Alternatively, save pMsg in NEWMESSAGE in a script local and append the subsequent data you get onto it.

Googie85
Posts: 226
Joined: Tue Aug 05, 2014 10:07 am

Re: File Send Over Base64.

Post by Googie85 » Mon Aug 19, 2024 9:48 am

Thanks LCMark it works!! I overlooked that!!

Many Thanks!!!!!!!!!

Googie.

Post Reply