2016-03-23 9 views
0

私のソケットでデータが破損しています。問題の内容がわかりません。ユーザーが録音した音声クリップを送信すると、送信するこの方法が有効になります。私がファイルを見ると、私は記録された元のファイルよりずっとずっと小さくなっています。ソケットでデータが破損しました

22更新:00/23/03 ** ENDを受信側

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


} 

を送ることがread()によって返されたカウントを無視して、

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

    } 

答えて

0

通常の問題です。コピーループは次のようになります。

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

内部のループをフラッシュしないでください。

EDITオブジェクトストリームを削除します。あなたはそれらを使用しているようには見えず、ヘッダーを送信し、帯域幅を無駄にします。同じ変数に受け入れる直前に新しいソケットを作成しないでください。それはリソースリークです。

+0

ありがとうございます。送信側または受信側を参照していますか?受信側をこれに変更して、バイトが一致したことに気付きました。しかし、WAVファイルはまだ再生されません。 – Sleeking

+0

私はコードを更新しました。以前と同じように動作しているように見えますか? – Sleeking

+0

どこから魔法の番号15123を取得しましたか?ファイルは同じ長さですか? – EJP

関連する問題