Skip to main content
Participant
April 7, 2015
Question

Socket AS3 ( Receive File .TXT ) + Java Socket

  • April 7, 2015
  • 0 replies
  • 283 views

Hi,

I have Java Socket sending .TXT file ( 10 MB ) . But the AS3 only gets 63 Kb . I do not understand the loss of bytes ...

Souce Java :

public void sendFile(String fileName) {

        try {

            //handle file read

            File myFile = new File(fileName);

            byte[] mybytearray = new byte[(int) myFile.length()];

            FileInputStream fis = new FileInputStream(myFile);

            BufferedInputStream bis = new BufferedInputStream(fis);

            DataInputStream dis = new DataInputStream(bis);

            dis.readFully(mybytearray, 0, mybytearray.length);

            OutputStream os = clientSocket.getOutputStream();

            byte[] fileSize = intToByte(mybytearray.length);

            byte[] clientData = new byte[(int) myFile.length() + mybytearray.length];

            System.arraycopy(fileSize, 0, clientData, 0, fileSize.length); // Copy to the file size byte array to the sending array (clientData) beginning the in the 0 index

            System.arraycopy(mybytearray, 0, clientData, 4, mybytearray.length); // Copy to the file data byte array to the sending array (clientData) beginning the in the 4 index

            DataOutputStream dos = new DataOutputStream(os);

            //dos.writeUTF(myFile.getName());

            //dos.writeInt(mybytearray.length);

            dos.write(clientData, 0, clientData.length);

            dos.flush();

}

AS3 Source..

private function onResponse(e:ProgressEvent):void {

  var file:File;

  var fs:FileStream;

  var fileData:ByteArray = new ByteArray();

  file = File.documentsDirectory.resolvePath("tmpReceive.txt");

  fs = new FileStream();

  fs.addEventListener(Event.CLOSE,onCloseFileReceive);

if(_socket.bytesAvailable > 0) {

  while(_socket.bytesAvailable) {

  // read the socket data into the fileData

  _socket.readBytes(fileData,0,0);

  }

  }

  fs.open(file, FileMode.WRITE);

  // Writing the file

  fs.writeBytes(fileData);

  fs.close();

This topic has been closed for replies.