2

I am trying to implement the voice chat using java multiCastSocket programming. Taking the input from the microphone and trying to listen it on the speaker. Problem is am not able to hear any voice back. I debugged through it wireshark and i can see that i am able to send packets to the multicast group address and i can see join message but there are no packets coming back from multicast group address. Can anyone please look at the code and let me know if am doing anything wrong? Thanks in advance.

/Sender code/

public class 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);
        }
    }
}

}

/Receiver Code/

public class 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();
    }
}

}

SMM
  • 39
  • 1
  • 6

1 Answers1

0

Try using the setReuseAddr() method on your sockets:

sock.setReuseAddress(true);

The JavaDocs say:

Enable/disable the SO_REUSEADDR socket option. For UDP sockets it may be necessary to bind more than one socket to the same socket address. This is typically for the purpose of receiving multicast packets (See MulticastSocket). The SO_REUSEADDR socket option allows multiple sockets to be bound to the same socket address if the SO_REUSEADDR socket option is enabled prior to binding the socket using bind(SocketAddress).

Note: This functionality is not supported by all existing platforms, so it is implementation specific whether this option will be ignored or not. However, if it is not supported then getReuseAddress() will always return false.

Jason Nichols
  • 11,603
  • 5
  • 34
  • 53
  • 1
    Hi, Thanks for your answer but i still get no packet back from multicast group address. Is there any other flaw in the program? – SMM Apr 16 '14 at 01:56