Send file from C# Server to AS3 Flash Clients using Sockets in a Local Network - with sample

4 posts

Flag Post

UPDATED & SOLVED

Hello everyone,

I wanted to send files from a C# server to AS3 Clients using sockets in a Local Network. I had some trouble finding out how to do it, but I managed to do so.

Server (C#):
1 – I create a TcpListener that listens for new clients with any IP in that network to the specified port number;
2 – When a new client connects I create a Thread to handle it;
3 – In that Thread I send the data I want to. In this case that data is divided in two parts, the first is a 4 bytearray that contains the size of the file I want to send, and the second is a bytearray of the file itself;
4 – After the data is sent I close that client connection;

Client (AS3):
1 – First of all I convert my bytearrays to LITTLE_ENDIAN, since AIR is by default BIG_ENDIAN and the data I get from the server is LITTLE_ENDIAN;
2 – Add the events to the socket connection and conect to the server;
3 – On the onResponse function I receive the socket packages to a bytearray;
4 – Save that bytearray into a file;

The last part on the client was the trickiest one, because it took me some time to figure out that AIR is BIG_ENDIAN by default, and how to read the packages. So basically, what I do is, on the first package that comes in I read the first 4 bytes to a bytearray and then convert that to an int, which gives me my total file size. I use this to know when there are no more packages to receive and therefore finish the connection and save the file. The rest of the first package and subsequent packages are added to a bytearray that will store that file data itself. The workaround here is to start writing on the beggining the first time a package is received and then add the subsequent packages where the last one left off, i.e., first time I write from the 0 to 65321, second one I’ll have to write from the 65321 to XXXX, and so on.

The file is being saved to the MyDocuments folder.

I’m unsure if this is the best method to do this, since I’m rather new to socket connection, however this works for me and I tested with files up to 165MB and it works. It supports multiple client connections and is pretty basic, but this is a starting point, not a finish line.

I hope this can help others as it helped me, since I did not find anything like it on the web (regarding file transfer not C# → AS3 connection).

If someone wants to input some info or needs clarification on something, please feel free to ask.

Last but no least, sample can be downloaded here: http://sdrv.ms/W5mSs9 (Server in C# Express 2010 and Client in Flash Builder 4.6 with Flex SDK 4.6.0)

 
Flag Post

You cannot save a file to flash – flash is running on a clients web browser (assuming ur not making an AIR game). The Flash sandbox will not allow you to write anything to the users hard drive (except for flash cookies – also called Shared Objects) The problem is Shared Objects need to be rather small as you have very limited space on what you can store (approx 100kb).

If you want to load a picture, try using the Loader class, and using the loadbytes function. This will let you load raw binary data into a loader. You can then make a bitmapdata object and call Draw on the loader to get a bitmapdata representation of your image.

 
Flag Post

I believe what ninjapaint said is correct. If not, google it.

 
Flag Post

Thank you ninjapaint for the reply. I forgot to mention that it is in fact an AIR application, so I have the permissions to write files on the system.

I managed to solve my problem and updated the first post with the answer and sample.