title: Send file from C# Server to AS3 AIR Clients using Sockets in a Local Network - with sample
body: <p>I want to send a file from a C# server to a AS3 Flash Client. My C# server code for sending the file is something like this:</p>
  
  <pre><code>IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
  Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  sock.Bind(ipEnd);
  sock.Listen(100);
  //clientSock is the socket object of client, so we can use it now to transfer data to client
  Socket clientSock = sock.Accept();
  
  // This part gets the file and send the data in bytearray
  byte[] fileData = File.ReadAllBytes("send/mypicture.jpg");
  clientSock.Send(fileData);
  </code></pre>
  
  <p>Now I need a as3 client. I found this: <a href="http://flasharp.blogspot.pt/2010/03/socket-serverclient-chat.html" rel="nofollow">http://flasharp.blogspot.pt/2010/03/socket-serverclient-chat.html</a> and I constructed something like this:</p>
  
  <pre><code>public function Main():void {
      ...
      socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
      ...
  }
  
  function onResponse(e:ProgressEvent):void {
      var file:File;
      var fs:FileStream;
      var fileData:ByteArray = new ByteArray();
  
      // Check if socket has data
      if(socket.bytesAvailable &gt; 0) {
          while(socket.bytesAvailable) {
              // read the socket data into the fileData
              socket.readBytes(fileData,0,0);
          }
      }
      file = File.documentsDirectory.resolvePath("teste.jpg");
      fs = new FileStream();
      fs.open(file, FileMode.WRITE);
      // Writing the file
      fs.writeBytes(fileData);
      fs.close();
  }
  </code></pre>
  
  <p>I've managed to send and receive a file, but it only saves up to 50kbs, anything bigger and you just gest a file with that size.</p>
  
  <p>Any thoughts on how to transfer a file with any size?</p>