简体   繁体   中英

JAVA app on voice calling

I am trying to develop a java app on voice call. the server side relays back the message to all the clients connected to it. Now the problem is even the client who has sent the message originally gets to hear his message back as the server relays the message back to all the clients due which we hear our own voice too. could any help me through this?

my server side code:

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:

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.

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.");
  }
}

the sound receiver class :

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

Take a look at the code we now check if the socket we want to send data to is the same socket that we read the data from and if so we skip it.

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(connection, inBytes, bytesRead);
        }
    }
    sockets.remove(connection);
}


public static void sendToAll(Socket connection, byte[] byteArray, int q)
{
    Iterator<socket> sockIt = sockets.iterator();
    while(sockIt.hasNext())
    {
        Socket temp = sockIt.next();
        if(connection == temp){
            continue;
        }
        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){}
      }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM