2017-02-14 2 views
0

私は、クライアントとサーバーの間のオーディオファイル(mp3)転送コードを記述しました。最初のサーバーが開始し、データグラムパケットが受信されるのを待ってから、クライアントはサーバーにUDPデータグラムパケットを送ります。JAVAでUDPを使用したオーディオファイル転送

\サーバー

クライアント\
  serverSocket = new DatagramSocket(4000); 
      int packetsize=1024; 
      FileOutputStream fos = null; 
      try { 
       fos = new FileOutputStream("zz.wav"); 
       BufferedOutputStream bos = new BufferedOutputStream(fos); 
       double nosofpackets=Math.ceil(((int) (new File("F:\\nirmal\\1.wav")).length())/packetsize); 
       byte[] mybytearray = new byte[packetsize]; 
       DatagramPacket receivePacket = new DatagramPacket(mybytearray,mybytearray.length); 

       System.out.println(nosofpackets+" "+mybytearray+" "+ packetsize); 

       for(double i=0;i<nosofpackets+1;i++) 
       { 

        serverSocket.receive(receivePacket); 
        byte audioData[] = receivePacket.getData(); 
        System.out.println("Packet:"+(i+1)); 
        bos.write(audioData, 0,audioData.length); 
       } 

File myFile = new File("F:\\nirmal\\1.wav"); 
    DatagramSocket ds=new DatagramSocket(9000); 
    DatagramPacket dp; 
    int packetsize=1024; 
    double nosofpackets; 
    noofpackets= =Math.ceil(((int) myFile.length())/packetsize); 

    BufferedInputStream bis; 
    bis = new BufferedInputStream(new= FileInputStream(myFile));   
    for(double i=0;i<nosofpackets+1;i++) 
     { 
      byte[] mybytearray = new byte[packetsize]; 
      bis.read(mybytearray, 0, mybytearray.length); 
      System.out.println("Packet:"+(i+1)); 
     dp=new DatagramPacket(mybytearray,mybytearray.length,InetAddress.getByName("172.17.13.46"),4000); 
        } 

両方のクライアントANSサーバは、サーバがserverSocket.receiveで動けなく実行されている(receivePacket)は、サーバのようなラインは、任意のpackets.Iを受​​信して​​いません私はどこでミスをしているのか分かりません。

答えて

0

問題はクライアント側にあります。

サーバー側で受け取ったDatagramSocketを通してDatagramPacketsを送信する必要があります。 DatagramScoketはクライアントソケットなので、ポート番号を予約する必要はありません。ローカルコンピュータ上でコードを実行した場合、より良い私は、コードをproovedすると、サーバーがクライアントからのすべてのパケットを受信することができませんでした"localhost"または"127.0.0.1"

public class UDPClient { 

    public void send() throws IOException, InterruptedException { 
     File myFile = new File("aa.hex"); 
     DatagramSocket ds = null; 
     BufferedInputStream bis = null; 
     try { 
      ds = new DatagramSocket(); 
      DatagramPacket dp; 
      int packetsize = 1024; 
      double nosofpackets; 
      nosofpackets = Math.ceil(((int) myFile.length())/packetsize); 

      bis = new BufferedInputStream(new FileInputStream(myFile)); 
      for (double i = 0; i < nosofpackets + 1; i++) { 
       byte[] mybytearray = new byte[packetsize]; 
       bis.read(mybytearray, 0, mybytearray.length); 
       System.out.println("Packet:" + (i + 1)); 
       dp = new DatagramPacket(mybytearray, mybytearray.length, InetAddress.getByName("127.0.0.1"), 4000); 
       ds.send(dp);      
      } 
     }finally { 
      if(bis!=null) 
       bis.close(); 
      if(ds !=null) 
       ds.close(); 
     } 

    } 
} 

を使用しています。 ds.send(dp)の後にThread.sleep(10L)命令を追加して修正しました。

+0

mp3またはwavファイルでテストしましたか? –

+0

バイナリファイルでテストしました。 原点ファイルsha1と運命ファイルを計算して2つのファイルを調べました。 SHA1 ---------------------------------------- ------ ------- ------------ 9574F1AE5946D8E6A86351389445FECF80242C25 aa.hex 9574F1AE5946D8E6A86351389445FECF80242C25 zz.hex あなたは、両方のファイルが同じSHAを持って見ることができるように私は使用して問題はないと思いますwoul mp3またはwavファイル。 – jmarques

関連する問題