Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to develop a Java voice call application. but i am facing a problem. whenever i am starting the application on server side and client side on two different PCs, i hear my own voice in my own PC if i am a client and nothing is being able to be heard on server side, its totally nonfunctional. the code seems to me as fine. could anyone help me through this?

Server side :

Java
import java.net.*;
import java.io.*;
import java.util.*;

public class Echo
{
    public static void main(String[] args) throws Exception
    {
        ServerSocket serverSocket = new ServerSocket(3000);
        while(true){Thread echoThread = new Thread(new EchoThread(serverSocket.accept()));
                    echoThread.start();}
    }
}

class EchoThread implements Runnable
{
    public static Collection<socket> sockets = new ArrayList<socket>();
    Socket connection = null;
    DataInputStream dataIn = null;
    DataOutputStream dataOut = null;

    public EchoThread(Socket conn) throws Exception
    {
        connection = conn;
        dataIn = new DataInputStream(connection.getInputStream());
        dataOut = new DataOutputStream(connection.getOutputStream());
        sockets.add(connection);
    }

    public void run()
    {
        int bytesRead = 0;
        byte[] inBytes = new byte[1];
        while(bytesRead != -1)
        {
            try{bytesRead = dataIn.read(inBytes, 0, inBytes.length);}catch (IOException e){}
            if(bytesRead >= 0)
            {
                sendToAll(inBytes, bytesRead);
            }
        }
        sockets.remove(connection);
    }

    public static void sendToAll(byte[] byteArray, int q)
    {
        Iterator<socket> sockIt = sockets.iterator();
        while(sockIt.hasNext())
        {
            Socket temp = sockIt.next();
            DataOutputStream tempOut = null;
            try
            {
                tempOut = new DataOutputStream(temp.getOutputStream());
            } catch (IOException e1)
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try{tempOut.write(byteArray, 0, q);}catch (IOException e){}
        }
    }
}





Client side:
here i have two classes. one takes microphone input, sends it to the server, and another takes data from the server, and plays that data out of a speaker.


Java
import java.io.DataOutputStream;
import java.net.*;
import javax.sound.sampled.*;

public class Program
{
    public final static String SERVER = JOptionPane.showInputDialog("Please enter server ip");
    public static void main(String[] args) throws Exception
    {
        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);
        Socket conn = new Socket(SERVER,3000);
        microphone.start();
        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
        int bytesRead = 0;
        byte[] soundData = new byte[1];
        Thread inThread = new Thread(new SoundReceiver(conn));
        inThread.start();
        while(bytesRead != -1)
        {
            bytesRead = microphone.read(soundData, 0, soundData.length);
            if(bytesRead >= 0)
            {
                dos.write(soundData, 0, bytesRead);
            }
        }
        System.out.println("IT IS DONE.");
    }
}





Java
import java.net.*;
import java.io.*;

import javax.sound.sampled.*;

public class SoundReceiver implements Runnable
{
    Socket connection = null;
    DataInputStream soundIn = null;
    SourceDataLine inSpeaker = null;

    public SoundReceiver(Socket conn) throws Exception
    {
        connection = conn;
        soundIn = new DataInputStream(connection.getInputStream());
        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);
    }

    public void run()
    {
        int bytesRead = 0;
        byte[] inSound = new byte[1];
        inSpeaker.start();
        while(bytesRead != -1)
        {
            try{bytesRead = soundIn.read(inSound, 0, inSound.length);} catch (Exception e){}
            if(bytesRead >= 0)
            {
                inSpeaker.write(inSound, 0, bytesRead);
            }
        }
    }
}
Posted
Updated 14-Jan-14 6:36am
v5

1 solution

Your code is working -

Server holds a collection of sessions, when a session receives a message it relays it to each of the clients attached to all the sessions.

I would firstly not send the data back to the sender.
Then, if you need to hear the chat on the server then run a client on that machine.

To prevent sending the audio back to the original client that sent it:

Java
Socket temp = sockIt.next();
// skip if this is the sender
if (temp == this.connection)
{
    continue;
}
 
Share this answer
 
v2
Comments
Sameer Mohanty 15-Jan-14 7:13am    
@nagy Vilmos- can you point where the data is being sent back to client? or is it the echo in sound?
Nagy Vilmos 15-Jan-14 7:45am    
It's the line
try{tempOut.write(byteArray, 0, q);}catch (IOException e){}
Sameer Mohanty 15-Jan-14 8:21am    
i tried your solution of running the client on the server side and deleting that line of code.. but after doing so i am not being able to hear anything. could you please test it and say?
Nagy Vilmos 15-Jan-14 8:23am    
The point is that line sends the data BACK. Leave the code AS IT WAS and just run an extra client on the server.
Sameer Mohanty 15-Jan-14 8:35am    
thanx. but i am hearing my voice back too. what to do about that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900