import java.net.*;
import javax.sound.sampled.*; public class Vox extends Thread{  
        
   public static final AudioFormat FORMAT = new AudioFormat(
                     AudioFormat.Encoding.PCM_SIGNED, 16000.0f, 16, 1, 2, 16000.0f,true);  
   public static final int PORT = 5000;      
   private DatagramSocket socket = null;
   private DatagramPacket packet = new DatagramPacket(new byte[]{1},1);
   public Vox(String args) {       
      try {        
         if(args=="") {    
            System.out.println("Waiting for partner...");        
            socket = new DatagramSocket(PORT);       
            socket.receive(packet);   
            socket.connect(packet.getAddress(),PORT);     
         } 
         else {      
            socket = new DatagramSocket(PORT);      
            socket.connect(InetAddress.getByName(args),PORT);
            System.out.println("Connectted.");
         }   
      }
                 
         catch (Exception exec) {      
                 
            System.out.println("Exception creating connection:");   
            exec.printStackTrace(System.out);   
         }    
      System.out.println("Connected to partner...");
   }
   
   public void run(){
      Record rec = new Record(socket); 
      Play ply = new Play(socket); 
      rec.start();  
      ply.start(); 
   } 
   public static void main(String args[]){
Vox vx=new Vox("");
vx.start();
}
}        
class Play implements Runnable 
{  
   public final int BUFFER = 8000;   
   private SourceDataLine line = null; 
   private DatagramSocket connection = null; 
   private Thread thread = null;    
           
   public Play(DatagramSocket socket) 
           
   {    
      connection = socket; 
   }  
           
   public void start() {    
           
      thread = new Thread(this);   
      thread.setName("Play");  
      thread.start();  
   }  
           
   public void stop() {      
           
      thread = null;  }    
           
   public void run() {    
           
      DataLine.Info info = new DataLine.Info(SourceDataLine.class, Vox.FORMAT);       
      try {      
         line = (SourceDataLine) AudioSystem.getLine(info);   
         line.open(Vox.FORMAT, BUFFER);  
      } 
                 
         catch (LineUnavailableException exec) {         
                 
            System.out.println("Play: Audio Output not ready.\n"+exec.getMessage());      
            System.exit(5);    } 
                 
         catch (Exception exec) {         
                 
            System.out.println("Play: Fatal Exception:\n"+exec.getMessage());  
            System.exit(6);    }    
      line.start(); 
      DatagramPacket packet = new DatagramPacket(new byte[65536],65536);
      System.out.println("Play started...");    
      while (thread != null) {      
         try {      
            connection.receive(packet);    
         }
                    
            catch(Exception exec) {        
                    
               System.out.println("Play: Exception receiving packet:\n"+exec.getMessage());    
            }    
         line.write(packet.getData(),0,packet.getLength()); 
         packet = new DatagramPacket(new byte[65536],65536);  
         Thread.yield();    }  
      line.stop(); 
      line.flush();  
      line.close(); 
      line = null;  
   }
}
       
class Record implements Runnable 
        
{
   private TargetDataLine line = null; 
   private DatagramSocket connection = null; 
   private Thread thread = null;  
           
   public Record(DatagramSocket socket) 
           
   { 
      connection = socket;  
   } 
           
   public void start() 
           
   {  
      thread = new Thread(this);   
      thread.setName("Record");   
      thread.start();  
   }  
           
   public void stop() {      
           
      thread = null;  } 
           
   public void run() {   
           
      DataLine.Info info = new DataLine.Info(TargetDataLine.class, Vox.FORMAT); 
      try {      
         line = (TargetDataLine) AudioSystem.getLine(info);   
         line.open(Vox.FORMAT, line.getBufferSize());  
      } 
                 
         catch (LineUnavailableException exec) {    
                 
            System.out.println("Record: Audio Input not ready.\n"+exec.getMessage ());    
            System.exit(3);    } 
                 
         catch
            (Exception exec) {      
                 
            System.out.println("Record: Fatal Exception:\n"+exec.getMessage());  
            System.exit (4);   
         }     
      int frameSizeInBytes = Vox.FORMAT.getFrameSize ();//获得每帧的大小   
      int bufferLengthInFrames = line.getBufferSize() / 8;  
      int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;   
      int minPacketSize =  (int)(bufferLengthInBytes/1.5); 
      byte[] data;   
      int bytesAvailable;    
      line.start ();   
      System.out.println("Record started...");   
      while (thread != null) {      // Send data only when we have a buffer of at least 50%   
         if ((bytesAvailable = line.available())>=minPacketSize)
         { 
            data = new byte[bytesAvailable];     
            line.read(data,0,bytesAvailable);         
            try {        
               connection.send(new DatagramPacket(data,bytesAvailable));      
            }
                       
               catch(Exception exec) {          
                       
                  System.out.println("Record: Exception sending packet:\n"+exec.getMessage());        }      }      
         try {       
            Thread.sleep(50);   
         }
                    
            catch(InterruptedException exec) {
                    
            } 
      }   
      line.stop();  
      line.flush();   
      line.close();    
      line = null;  
   }
}在本机测试的时候可以顺利接受,但是声音里也有杂音,在可以接受的范围。在局域网中,
杂音就非常的大,是一种尖锐刺耳的杂音。
请大家看看是怎么回事,谢谢~