2

我正在尝试使用 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();
    }
}

}

4

1 回答 1

0

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

sock.setReuseAddress(true);

JavaDocs 说:

启用/禁用 SO_REUSEADDR 套接字选项。对于 UDP 套接字,可能需要将多个套接字绑定到同一个套接字地址。这通常用于接收多播数据包(请参阅 MulticastSocket)。如果在使用 bind(SocketAddress) 绑定套接字之前启用了 SO_REUSEADDR 套接字选项,则 SO_REUSEADDR 套接字选项允许将多个套接字绑定到同一个套接字地址。

注意:并非所有现有平台都支持此功能,因此是否忽略此选项取决于具体实现。但是,如果不支持,则 getReuseAddress() 将始终返回 false。

于 2014-04-15T20:20:13.873 回答