套接字数据损坏

时间:2016-03-23 02:19:51

标签: java android sockets stream

我的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();

    }

1 个答案:

答案 0 :(得分:0)

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

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

并且不要在内圈冲洗。

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