简体   繁体   中英

Problem with socket communication [Android]

I'm programming a server-client application and I have a problem when transferring to the device a song as a binary byte array from the server.

The code that I use is the next:

int bytesRead = 0;
        FileOutputStream fos = new FileOutputStream(file);
        DataOutputStream dosToFile = new DataOutputStream(fos);
        long totalBytesWritten = 0;
        byte[] buffer = new byte[5024]; 
        do {
            bytesRead = dis.read(buffer, 0, 5024);
            if ( bytesRead > 0) {
                dosToFile.write(buffer, 0, bytesRead);
                dosToFile.flush();
                totalBytesWritten += bytesRead;             
                Log.e("", "Total Bytes written = "+ totalBytesWritten);
            } else if ( bytesRead == 0 ) {
                Log.e("","Zero bytes readed when downloading song.");
            } else if ( bytesRead == -1 ) {
                Log.e("","Read returned -1 when downloading song.");
            }
        } while ( bytesRead > -1 );

The problem comes when the song is already downloaded. In the last read, after reading the last bytes of the song (and writing them to the sdcard), the application freezes in the read and it does not return the supposed -1.

Is the code shown wrong? Should I do the transfer in any other way?

I send my binary data with this code:

byte [] mybytearray  = new byte [(int)myFile.length()];
        mybytearray = this.fileToByteArray(myFile);
        if ( mybytearray != null ) {
            dos.write(mybytearray, 0, mybytearray.length);
            dos.flush();
            System.out.println("Song send.");
        } else {
            System.out.println("The song could not be send.");
        }

Thank you very much.

Try this:

int read = nis.read(buffer, 0, 4096); // This is blocking

while (read != -1) {
byte[] tempdata = new byte[read];
System.arraycopy(buffer, 0, tempdata, 0, read);

// Log.i(NTAG, "Got data: " + new String(tempdata));
handler.sendMessage(handler.obtainMessage(MSG_NETWORK_GOT_DATA, tempdata));
read = nis.read(buffer, 0, 4096); // This is blocking
}

The handler is just my way of sending the message to be parsed (or written to a file). You can do whatever here. When its done reading its just done reading, no need to check within the loop. You can catch Excecptions such as SocketTimeoutException, etc... to determine what the problem might be.

Solution:

        int bytesRead = 0;
        FileOutputStream fos = new FileOutputStream(file);
        DataOutputStream dosToFile = new DataOutputStream(fos);
        long totalBytesWritten = 0;
        byte[] buffer = new byte[5024];     // 8Kb 
        do {
            bytesRead = dis.read(buffer, 0, 5024);
            if ( bytesRead > 0) {
                dosToFile.write(buffer, 0, bytesRead);
                dosToFile.flush();
                totalBytesWritten += bytesRead;             //Se acumula el numero de bytes escritos en el fichero
            } else if ( bytesRead == 0 ) {
                Log.e("","Zero bytes readed when downloading song.");
            } else if ( bytesRead == -1 ) {
                Log.e("","Read returned -1 when downloading song.");
            }
            if ( totalBytesWritten == fileLength ) break;
        } while ( bytesRead > -1 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM