【问题标题】:Problem with socket communication [Android]套接字通信问题[Android]
【发布时间】:2011-08-03 19:05:34
【问题描述】:

我正在编写服务器-客户端应用程序,但在将歌曲作为二进制字节数组从服务器传输到设备时遇到问题。

我使用的代码是下一个:

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 );

当歌曲已经下载时,问题就来了。在最后一次读取中,在读取歌曲的最后一个字节(并将它们写入 sdcard)之后,应用程序在读取中冻结并且它不返回假定的 -1。

显示的代码有误吗?我应该以其他方式进行转移吗?

我用这个代码发送我的二进制数据:

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.");
        }

非常感谢。

【问题讨论】:

    标签: android sockets download communication


    【解决方案1】:

    试试这个:

    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
    }
    

    处理程序只是我发送要解析(或写入文件)的消息的方式。你可以在这里做任何事情。当它完成阅读时,它刚刚完成阅读,无需在循环内检查。您可以捕获 SocketTimeoutException 等异常,以确定问题可能是什么。

    【讨论】:

    • 你好,杰克。在最后一次读取调用时它仍然阻塞。我编辑了我的第一篇文章,向您展示了我如何从服务器发送数据。谢谢
    • 最后,我解决了这个问题,将写入的字节与文件的总长度进行比较,如果它们相等则打破循环。
    【解决方案2】:

    解决方案:

            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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      相关资源
      最近更新 更多