Timing over socket connection

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

trevix
Posts: 1077
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Timing over socket connection

Post by trevix » Wed Dec 21, 2022 4:19 pm

Here again...
Since I read somewhere that the "Write to socket" happens all at once, I modified your stack so that I can use it not only for sending files, but eventually to send text and arrays (not implemented yet).
It looks like it is working but the received file is slightly corrupted (jpg around 100k).
I think the problem is in the base64Encode-decode.
I tried several variations, like textEncode(base64Encode(tFileContent),"UTF-8") and the corresponding decode, but nothing seems to work.

If you wonder why I have chosen 59976 as kChunkSize, it is because is a multiple of 72 (num of chars in each line of base64Encode). Just an experiment trying solving the corrupted received file.

Any idea?
Thanks
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

trevix
Posts: 1077
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Timing over socket connection

Post by trevix » Wed Dec 21, 2022 4:21 pm

Forgot to add the stack
Attachments
SocketsTestApp2.livecode.zip
(4.84 KiB) Downloaded 142 times
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

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

Re: Timing over socket connection

Post by FourthWorld » Wed Dec 21, 2022 5:12 pm

Browsers reliably deliver images over TCP. Images are binary. How do they do that?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

trevix
Posts: 1077
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Timing over socket connection

Post by trevix » Wed Dec 21, 2022 5:56 pm

I ain't no google :D
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

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

Re: Timing over socket connection

Post by FourthWorld » Wed Dec 21, 2022 6:08 pm

It wasn't a request. It was a solutions prompt.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

trevix
Posts: 1077
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Timing over socket connection

Post by trevix » Thu Dec 22, 2022 11:01 am

Sorry: I am not sure to understand...
I know that it is possible and that it is me that I am missing some...
I compared data in the send and in the receive, but my multi socket reading apparently doesn't do a good job.
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

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

Re: Timing over socket connection

Post by FourthWorld » Thu Dec 22, 2022 5:53 pm

trevix wrote:
Thu Dec 22, 2022 11:01 am
I compared data in the send and in the receive, but my multi socket reading apparently doesn't do a good job.
Do you need multiple sockets?

How do others accomplish this?

What is the largest data size you expect to send?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

trevix
Posts: 1077
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Timing over socket connection

Post by trevix » Thu Dec 22, 2022 6:30 pm

- yes, more then one user can connect to the server and send data. When I said "multi socket reading" I meant that the reading happens in several chunks. Sorry.
In fact I wonder if every reiteration of socket receiving the data and placing in local vars, is a separate instance. Do I have to take in account what happens with 2 users sending data in the same time, and have each one with its own set of vars?

- I had it working using "read socket until <SomeChar>" but it was too slow. Now I am trying to do 1 send and multiple reading. That is I send a header specifying how many time to read 59976 chars (of a base64Encoded) and the leftOver.

- around 300k is the max file size

I actually SOLVED my problem, just now:
For every chunk of data received I was doing a

Code: Select all

put base64Decode(pData) into tdata
put tdata after url ("binfile:" & sFileName)
I guess it is a no,no...
Putting the decoded data after a local var and, only at the end, saving to file, seems to have resolved.
I will post a working example (I guess it must be used with two computers, but I am not sure)
Thanks
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

trevix
Posts: 1077
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Timing over socket connection

Post by trevix » Thu Dec 22, 2022 6:38 pm

Here is a working example, very fast
Attachments
SocketsTestApp3.livecode.zip
(4.9 KiB) Downloaded 135 times
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

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

Re: Timing over socket connection

Post by FourthWorld » Thu Dec 22, 2022 7:36 pm

trevix wrote:
Thu Dec 22, 2022 6:38 pm
Here is a working example, very fast
Well done. Glad you have that working.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Cairoo
Posts: 112
Joined: Wed Dec 05, 2012 5:54 pm

Re: Timing over socket connection

Post by Cairoo » Wed Jan 11, 2023 5:57 pm

Yes Trevix, my example stack's variables was only for one client connection at a time. I hoped to just show some basic steps for sending a file over a socket. To scale it for multiple connections, one would have to use arrays for storing the values associated with each connection. Maybe use the socket ID for the array keys.
The default socket timeout is usually several hours. You can set your own timeout if you want to and then perhaps add some code to handle a timeout.

trevix
Posts: 1077
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Timing over socket connection

Post by trevix » Wed Jan 11, 2023 6:03 pm

Yes thanks, I provided about that...

Now I have bi-directional communication between the client and the server and it is quite fast thanks to you.
Sometime, though, it gets stuck on the Write to socket, for many seconds.
Can you confirm me that, in the script, each "write to socket" must be followed by a "read from socket"?
And, having bi-directionality, it must be done both on the client and in the server?
Or the first "read from socket" after establishing the connection is enough?

Thanks for any help
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

Cairoo
Posts: 112
Joined: Wed Dec 05, 2012 5:54 pm

Re: Timing over socket connection

Post by Cairoo » Wed Jan 11, 2023 7:30 pm

AFAIK, any data written to the socket waits in a buffer on the receiving end until it's read or the socket is closed.

trevix
Posts: 1077
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Timing over socket connection

Post by trevix » Wed Jan 11, 2023 7:36 pm

And having content in the buffer is an obstacle to writing to socket?
More read then necessary is a problem? even if the buffer is empty?
Is there a buffer out and a buffer in?
:D :D :D :oops:
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

Cairoo
Posts: 112
Joined: Wed Dec 05, 2012 5:54 pm

Re: Timing over socket connection

Post by Cairoo » Wed Jan 11, 2023 8:38 pm

Not until the buffer is full on the receiving end. If the buffer is full, no more data can be written until some data is read. I don't know the size of the buffer, though.
There's two buffers, one in each direction, so both ends can receive data. Each end clears data from its own receiving buffer by reading from it.
If you try to read an empty buffer you'll just get an empty value unless you told it how much to read or to read until it encounters a specified chunk, in which case it will wait until the specified amount of data is received or until the chunk is encountered.

Post Reply