Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Voice Chat using java multicastSocket

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Voice Chat using java multicastSocket

    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 hear any voice back. I debugged through it wireshark and i am receiving the packets from the multicast group address. Looks like there is some problem while i am trying to get the sound out of speaker. Can someone please help me out. I have pasted my code below. Thanks in advance.

    /*Sender Code*/
    public class MulticastAudioSender {
    	/** Port to use for IP Multicast sending. */
        public static final int IPM_PORT = 7778;
     
        /** Address to use for IP Multicast sending. */
        public static final String IPM_ADDRESS = "235.1.1.1";
     
        /** Message text to send. */
        public static final String IPM_MSG = "Hello UTD!!";
     
        /** Time to wait between sending messages, in milliseconds. */
        public static final int SLEEP_MILLISECS = 1000;
     
        /** UDP Socket object to use for networking. */
        private MulticastSocket sock;
     
        /** UPD Packet object to use for sending message. */
        private DatagramPacket sendPack;
     
        /** Size of receive buffer, in bytes. */
        public static final int BUFFER_SIZE = 20000;
     
        /** Default constructor: sets up networking. */
        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);
    			}
    		}
        }
    }

     
    public class IPMulticastReceiver {
     
        /** Port to use for IP Multicast receiving. */
        public static final int IPM_PORT = 7778;
     
        /** IP Multicast address to receive on. */
        public static final String IPM_ADDRESS = "235.1.1.1";
     
        /** Size of receive buffer, in bytes. */
        public static final int BUFFER_SIZE = 200;
     
        /** IP Multicast Socket object to use. */
        private MulticastSocket sock;
     
        /** UDP Packet object to use for receiving message. */
        private DatagramPacket pack;
     
        /** Buffer to store received text in. */
        private byte[] receiveBuffer;
     
        byte[] inSound;
    	SourceDataLine inSpeaker = null;
     
        /** Default constructor: sets up IP MultiCast Receiving. */
        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();
            }
        }
    }
    Last edited by sunil104.munavalli@gmail.; April 13th, 2014 at 12:55 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,741
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Voice Chat using java multicastSocket

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

Similar Threads

  1. Chat using TCP/IP, chat text,Voice,Icon,send file ....
    By hien281191 in forum Totally Off Topic
    Replies: 2
    Last Post: September 13th, 2013, 02:56 PM
  2. Chat webcam and voice
    By hien281191 in forum Java Networking
    Replies: 3
    Last Post: April 3rd, 2013, 11:21 PM
  3. Voice chat
    By sana'a in forum Java Networking
    Replies: 3
    Last Post: October 11th, 2011, 04:14 PM
  4. sending Voice/audio Packet in java
    By kollyisrealisaac in forum Java Networking
    Replies: 1
    Last Post: June 7th, 2011, 08:53 AM
  5. voice chat in java
    By kollyisrealisaac in forum Java Networking
    Replies: 1
    Last Post: June 6th, 2011, 04:15 AM

Tags for this Thread