【问题标题】:java voice chat using multicastsocket programmingjava语音聊天使用multicastsocket编程
【发布时间】:2014-05-30 09:18:07
【问题描述】:

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

}

【问题讨论】:

    标签: java audio chat voice multicastsocket


    【解决方案1】:

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

    sock.setReuseAddress(true);
    

    JavaDocs 说:

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

    注意:并非所有现有平台都支持此功能, 所以这个选项是否会被忽略是特定于实现的 或不。但是,如果不支持,则 getReuseAddress() 将 总是返回 false。

    【讨论】:

    • 您好,感谢您的回答,但我仍然没有从多播组地址收到任何数据包。程序还有其他缺陷吗?
    猜你喜欢
    • 2014-11-03
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多