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