【问题标题】:Corrupted Data Over Socket [duplicate]套接字上的数据损坏[重复]
【发布时间】:2016-03-23 02:19:51
【问题描述】:

我通过我的 Socket 获取损坏的数据我真的不明白问题出在哪里。我发送一个语音剪辑,用户在放手时录制它,它在下面激活了它发送的这个方法。当我查看发送的文件时,它总是比我记录的原始文件小很多。

22:00/23/03 更新 **发送结束

 public void sendVoice() throws IOException{


    System.out.println("send voice");
    Socket connection2 = new Socket(InetAddress.getByName("127.0.0.1"),1200);
    System.out.println(connection2);

    ObjectOutputStream output2 = new ObjectOutputStream(connection2.getOutputStream());
    output2.flush();
    ObjectInputStream  input2 = new ObjectInputStream(connection2.getInputStream());
     System.out.println(input2);


                DataOutputStream dos = null;
                try {
                    dos = new DataOutputStream(connection2.getOutputStream());
                } catch (IOException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
                }
        FileInputStream fis = null;
                try {
                    fis = new FileInputStream(voiceOutput);

                } catch (FileNotFoundException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);

                }
         // new       
        int count = 0;
        byte[] buffer = new byte[4096];

                try {
while ((count = fis.read(buffer)) > 0) {
                        dos.write(buffer,0,count); 
                        dos.flush();
                        System.out.println(fis);
                    }
                } catch (IOException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
                }

                try {

                    fis.close();
                } catch (IOException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
                }
                try {
                    dos.flush();
                    dos.close();

                  ////        
                } catch (IOException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
                }


}

接收结束是

 public void downloadFile() throws IOException {
           server2 = new ServerSocket(1200,100);    
        System.out.println("downloading file.........");
       Socket connection2 = new Socket();
     connection2 = server2.accept();

     ObjectOutputStream output2 = new ObjectOutputStream(connection2.getOutputStream());
     output2.flush();
     ObjectInputStream   input2 = new ObjectInputStream(connection2.getInputStream());

DataInputStream dis = new DataInputStream(connection2.getInputStream());

    FileOutputStream fos = new FileOutputStream("voiceClip.wav");

    byte[] buffer = new byte[4096];
   System.out.println(buffer.length);
    //int filesize = 15123; 
    int read = 0;

   //int remaining = filesize;

   while ((read = dis.read(buffer)) > 0)
{
    fos.write(buffer, 0, read);
}


    /*
    while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
        totalRead += read;
        remaining -= read;
        System.out.println("read " + totalRead + " bytes.");
        fos.write(buffer, 0, read);
    }
*/
    fos.close();
    dis.close();

    }

【问题讨论】:

    标签: java android sockets stream


    【解决方案1】:

    常见问题,忽略read() 返回的计数。您的复制循环应如下所示:

    while ((count = in.read(buffer)) > 0)
    {
        out.write(buffer, 0, count);
    }
    

    并且不要在循环内部刷新。

    EDIT 并摆脱对象流。您似乎没有使用它们,并且它们会发送标头,这会浪费带宽。并且不要在接受同一个变量之前创建一个新的 Socket。那是资源泄漏。

    【讨论】:

    • 谢谢,你指的是发送端还是接收端我确实把接收端改成了这个,发现字节匹配了..但是WAV文件仍然无法播放
    • 我已经更新了我的代码,这似乎仍然不起作用并且行为方式与以前相同?
    • 你从哪里得到神奇的数字 15123?文件的长度是否相同?
    • 这个数字是任意的,没有任何目的,我只是想测试一下,希望有一些工作,但它没有,这个数字应该被删除然后我接受它。
    • 该数字用于控制您的读取大小。如果文件较长,它最终会将读取大小减小到零,这将导致短传输。在Math.min() 调用中使用此类代码的唯一用途是,如果您在传输之前确实知道文件大小,如重复问题中所示。否则,您应该只使用我的答案中的代码。
    猜你喜欢
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    相关资源
    最近更新 更多