I'm trying to write some data to a raw data socket(around 22 MB's). The scenario is such:-

1. Open local file
2. Read a chunk of bytes.
3. Write it to the Socket
4. Repeat 2 & 3 until the end of the file.

Now the problem is that my code(Below) is not transferring the complete file. It transfers maybe 3 out the the 22 MB with my test file. The trace however is complete and shows complete data being transmitted. I suspect that maybe it starts writing the next chunk before finishing the current one(though I'm not sure).

```
while(fs.bytesAvailable > 0){
    var readAmount = (fs.bytesAvailable < socketBufferSize) ? fs.bytesAvailable : socketBufferSize;
    seq++;
    air.trace(">"+seq+" WritePacket "+readAmount+" "+fs.position+" "+fs.bytesAvailable);
    fs.readBytes(bytes, 0, readAmount);
    air.trace(bytes.length);
    socket2.writeBytes(bytes, 0, bytes.length);
    socket2.flush();
}
fs.close();
socket2.close();

```

Above is the code that I'm supposed to be using. I would like to know if I'm doing anything right/wrong.

Inserting a forced delay between write iterations ensures that the file gets completely transferred indicated in the snippet below. However this is not an acceptable solution. I would like to know if there is some event I should be subscribing to or if anything needs to be done differently. The server on the other end is FileZilla FTP Server.

```
var sendData = function (){
    if(fs.bytesAvailable > 0){
    var readAmount = (fs.bytesAvailable < socketBufferSize) ? fs.bytesAvailable : socketBufferSize;
    seq++;
    air.trace(">"+seq+" WritePacket "+readAmount+" "+fs.position+" "+fs.bytesAvailable);
    fs.readBytes(bytes, 0, readAmount);
    air.trace(bytes.length);
    socket2.writeBytes(bytes, 0, bytes.length);
    socket2.flush();
    }
    else{
        air.trace("Closing Connection");
        fs.close();
        socket2.close();
    }
}
var interval = setInterval(sendData, 100);

```

Thanks