使用multicastsocket编程的java语音聊天

时间:2014-04-15 20:11:42

标签: java audio chat voice multicastsocket

我正在尝试使用java multiCastSocket编程实现语音聊天。从麦克风获取输入并尝试在扬声器上收听。问题是我无法听到任何声音。我通过它调试wireshark,我可以看到我能够将数据包发送到多播组地址,我可以看到加入消息,但没有从多播组地址返回的数据包。任何人都可以看看代码,让我知道我做错了什么?提前谢谢。

/ 发件人代码 /

公共类MulticastAudioSender {

public static final int IPM_PORT = 7778;

public static final String IPM_ADDRESS = "235.1.1.1";

public static final int SLEEP_MILLISECS = 1000;

private MulticastSocket sock;

private DatagramPacket sendPack;

public static final int BUFFER_SIZE = 20000;

public MulticastAudioSender(){

    try {
        sock = new MulticastSocket();
    } catch( Exception e ) {
        System.out.println( "Exception in creating socket or packet: "
                + e.getMessage() );
    }
}

private void sendMessage(byte[] soundData, int length) {
    try {
        sendPack = new DatagramPacket( soundData, length,
                InetAddress.getByName( IPM_ADDRESS ), IPM_PORT );
        sock.send( sendPack );
        //System.out.println( "Message sent." );
    } catch( Exception e ) {
        System.out.println( "Exception in sending packet: "
                + e.getMessage() );
    }
}

public static void main( String[] args ) throws Exception{

    MulticastAudioSender sender = new MulticastAudioSender();
    AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, af);
    TargetDataLine microphone = (TargetDataLine)AudioSystem.getLine(info);
    microphone.open(af);
    microphone.start();
    int bytesRead = 0;
    byte[] soundData = new byte[1];
    //sender.receiveMessage();
    while(bytesRead != -1) {
        bytesRead = microphone.read(soundData, 0, soundData.length);
        if(bytesRead >= 0){
            sender.sendMessage(soundData, soundData.length);
        }
    }
}

}

/ 接收者代码 /

公共类IPMulticastReceiver {

public static final int IPM_PORT = 7778;

public static final String IPM_ADDRESS = "235.1.1.1";

public static final int BUFFER_SIZE = 200;

private MulticastSocket sock;

private DatagramPacket pack;

private byte[] receiveBuffer;

byte[] inSound;
SourceDataLine inSpeaker = null;

public IPMulticastReceiver(){

    try {
        sock = new MulticastSocket( IPM_PORT );
        sock.joinGroup( InetAddress.getByName( IPM_ADDRESS ) );

        AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        inSpeaker = (SourceDataLine)AudioSystem.getLine(info);
        inSpeaker.open(af);

    } catch( Exception e ) {
        System.out.println( "Exception in creating Socket or Packet: "
                + e.getMessage() );
    }
}

private void receiveMessage() {
    try {
        receiveBuffer = new byte[BUFFER_SIZE];
        //sock.joinGroup(InetAddress.getByName(IPM_ADDRESS));
        pack = new DatagramPacket( receiveBuffer, receiveBuffer.length );
        sock.receive( pack );
        /*Thread inThread = new Thread(new MultiCastSoundReceiver(receiveBuffer));
        inThread.start();*/
        inSpeaker.write(receiveBuffer, 0, receiveBuffer.length);
        inSpeaker.drain();
    } catch( Exception e ) {
        e.printStackTrace();
        System.out.println( "Exception in receiving packet: "
                + e.getMessage() );
    }
}

public static void main( String[] args ) throws LineUnavailableException {
    IPMulticastReceiver receiver = new IPMulticastReceiver();

    while( true ) {
        receiver.receiveMessage();
    }
}

}

1 个答案:

答案 0 :(得分:0)

尝试在套接字上使用setReuseAddr()方法:

sock.setReuseAddress(true);

JavaDocs说:

  

启用/禁用SO_REUSEADDR套接字选项。对于UDP套接字,它可以   必须将多个套接字绑定到同一个套接字地址。   这通常是为了接收多播数据包(参见   MulticastSocket时)。 SO_REUSEADDR套接字选项允许多个   如果是SO_REUSEADDR,则套接字绑定到同一个套接字地址   在使用绑定套接字之前启用套接字选项   绑定(SocketAddress的)。

     

注意:所有现有平台都不支持此功能,   所以具体实现是否会忽略此选项   或不。但是,如果不支持,则getReuseAddress()将支持   总是返回假。