file transfer over sockets

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: heatherlaine, Klaus, FourthWorld, kevinmiller

windowsLC
Posts: 26
Joined: Sat Dec 16, 2017 5:58 am

file transfer over sockets

Post by windowsLC » Wed Dec 27, 2017 5:44 am

I've never done anything with sockets before so I've started playing with the chat server and client stacks a little. One thing I wanted to try and tackle was file transfer. I can send small files, but I can see as the files get bigger I'm probably going to need to send/receive small chunks of the file. I haven't found anything in searching, but are there any existing stacks or code examples of this around? baring that are there any best practices for this, like best bunk sizes and the best way to write the loop scripts for the read and write?

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1680
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: file transfer over sockets

Post by [-hh] » Wed Dec 27, 2017 12:17 pm

You could start from here:
Use "Sample stacks" from the LC toolbar. Search for sockets (simplest socket client/ simplest socket server).

There is no *overall* optimal chunk size, depends on server, client and object to transfer.
shiftLock happens

bogs
Posts: 1733
Joined: Sat Feb 25, 2017 10:45 pm

Re: file transfer over sockets

Post by bogs » Wed Dec 27, 2017 2:26 pm

Along with -hh's suggestion, you may want to read an article or two about MTU. The first is very cut down, but certainly explains the essentials, and should help you determine optimal size.
Image

MaxV
Posts: 1488
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: file transfer over sockets

Post by MaxV » Wed Dec 27, 2017 4:57 pm

windowsLC wrote:
Wed Dec 27, 2017 5:44 am
I've never done anything with sockets before so I've started playing with the chat server and client stacks a little. One thing I wanted to try and tackle was file transfer. I can send small files, but I can see as the files get bigger I'm probably going to need to send/receive small chunks of the file. I haven't found anything in searching, but are there any existing stacks or code examples of this around? baring that are there any best practices for this, like best bunk sizes and the best way to write the loop scripts for the read and write?
Here a guide: http://livecodeitalia.blogspot.it/2017/08/sockets.html

There is the translate button in the top left of the page.

Regarding size, in a private LAN, you should not have any problem with any size.
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

windowsLC
Posts: 26
Joined: Sat Dec 16, 2017 5:58 am

Re: file transfer over sockets

Post by windowsLC » Wed Dec 27, 2017 11:08 pm

Thanks for all the replies.

I have been able to make the client and the server following the demo apps. I am able to send and receive the small chat messages just fine. What I can't seem to do is send a binary file across. What I have tried:

I've opened an image file on the client side for binary read and read it into a field. Then I try to write that field to the socket.

On the server side, I am reading from the socket and putting that into a field. I tried "until EOF" on the read but not enough data appears to be read and put into the field. I tried looping it with more reads "until EOF" and I get more data but It doesn't seem to match the data in the client field.

I've looked through all the suggested info but I see no info on sending files over sockets, just text, and chat which I already can do.

UPDATE: I am now able to get the data across and the two fields match by using until return instead of EOF. Other then the word false being added (not sure what that is about). I will now try and write the data to a file.
Last edited by windowsLC on Wed Dec 27, 2017 11:29 pm, edited 1 time in total.

bogs
Posts: 1733
Joined: Sat Feb 25, 2017 10:45 pm

Re: file transfer over sockets

Post by bogs » Wed Dec 27, 2017 11:24 pm

I believe you could manipulate pictures by using textData (see dictionary)
Fully Supported Formats
Setting the text to one of these formats displays the image as expected and sets the paintCompression to the correct type automatically. The data is also preserved meaning that getting the text returns the original data unchanged.
- GIF (All platforms)
- JPEG (All platforms)
- PNG (All platforms)
- WMF (Windows only - Windows Metafile)
- EMF (Windows only - Windows Enhanced Metafile)
Maybe that will allow you to xfer the picture, and control how much of it is sent at a time (not sure, never tried it).
Image

windowsLC
Posts: 26
Joined: Sat Dec 16, 2017 5:58 am

Re: file transfer over sockets

Post by windowsLC » Wed Dec 27, 2017 11:37 pm

Thanks, bogs, I posted an update to my previous post before I saw your reply.

Your info looks good for images I'll look into it. I've also thought about base64 encoding first, but right now I'm going with the premise that if it is just over LAN I shouldn't need to worry about chunk size yet (which I think I found on a C++ forum that the TCP limit of 1500 is best). Right now I'm going see if my received data will write to a file.

bogs
Posts: 1733
Joined: Sat Feb 25, 2017 10:45 pm

Re: file transfer over sockets

Post by bogs » Wed Dec 27, 2017 11:54 pm

You might also look at BvG's sample chat client (I think user samples). Although I never used it, his stuff is always good to parse :D
Image

windowsLC
Posts: 26
Joined: Sat Dec 16, 2017 5:58 am

Re: file transfer over sockets

Post by windowsLC » Thu Dec 28, 2017 12:35 am

Amazed! I got it to work. I can send the file from client to server and the server can create the file.

I'm definitely going to have to figure out something for larger files though, the overhead of the current method is a lot I think because of putting the whole file in memory, better to read and write in chunks, I think

bogs
Posts: 1733
Joined: Sat Feb 25, 2017 10:45 pm

Re: file transfer over sockets

Post by bogs » Thu Dec 28, 2017 3:55 am

Congrats :)
Image

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5895
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: file transfer over sockets

Post by FourthWorld » Thu Dec 28, 2017 6:19 am

Rather than invent your own protocol, have you considered HTTP?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

windowsLC
Posts: 26
Joined: Sat Dec 16, 2017 5:58 am

Re: file transfer over sockets

Post by windowsLC » Fri Dec 29, 2017 12:07 am

I'm open to it FourthWorld and thought about it. Just not really sure how to implement HTTP in this LAN server to client setup.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5895
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: file transfer over sockets

Post by FourthWorld » Sat Dec 30, 2017 5:13 am

windowsLC wrote:
Fri Dec 29, 2017 12:07 am
I'm open to it FourthWorld and thought about it. Just not really sure how to implement HTTP in this LAN server to client setup.
What else is on the server? Do you have Apache or Lighttpd there?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

windowsLC
Posts: 26
Joined: Sat Dec 16, 2017 5:58 am

Re: file transfer over sockets

Post by windowsLC » Sun Dec 31, 2017 11:27 pm

Fourthworld, I've created a server and a client program like in all the Chat demo stacks around. I will need file distribution abilities along with the chat which is what I have started with sending the files over the sockets. the use case for this is purely over a local LAN and I won't have an existing web server at my disposal.

I think I can guess where you are going with this and I have thought about a small web server I can run from shell but ideally, I would like to keep this purely livecode as I started this project as a way to get my self back into using livecode.

I do think I remember people have created http servers in livecode before, that might interest me if I could create one that the only purpose is to allow the download of the files I need to distribute.

tfabacher
Posts: 13
Joined: Mon Mar 03, 2014 8:31 pm

Re: file transfer over sockets

Post by tfabacher » Wed Jan 31, 2018 9:40 am

Hello windowsLC...Did you find the solution? We are working on this also. We need to send a 1MB file over sockets.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”