Page 1 of 1

File Send Over Base64.

Posted: Mon Aug 19, 2024 3:23 am
by Googie85
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.

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 3:50 am
by Googie85
I am using this for an Android device, but I guess it falls under Windows development. Please move to an appropriate forum if inclined.

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 3:59 am
by FourthWorld
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?

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 4:05 am
by Googie85
I am missing 2 bytes.

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 4:16 am
by Googie85
When I open the received file in Notepad, it displays in Chinese.

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 4:32 am
by FourthWorld
Instead of reading to a specified character, have you tried reading for the length of the data?

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 4:51 am
by Googie85
The number of chars sent is 718149
The number of chars received is 716689

A difference of 1460

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 5:36 am
by FourthWorld
What is the number of bytes in each?

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 5:43 am
by Googie85
When I use, put the number of bytes in var, I get the same.

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 5:55 am
by Googie85
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

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 5:57 am
by Googie85
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

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 8:06 am
by AndyP
Have a look at this post

viewtopic.php?t=13424 and see if it helps.

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 8:20 am
by Googie85
Thanks heaps for your reply AndyP!!! Unfortunantely I cannot find a workable solution from it.

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 9:36 am
by LCMark
@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.

Re: File Send Over Base64.

Posted: Mon Aug 19, 2024 9:48 am
by Googie85
Thanks LCMark it works!! I overlooked that!!

Many Thanks!!!!!!!!!

Googie.