语音通话的JAVA应用程序

时间:2014-01-16 10:26:40

标签: java sockets

我正在尝试在语音通话上开发一个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){}
      }
  }
 }

客户方:

客户方: 这里我有两节课。一个接收麦克风输入,将其发送到服务器,另一个接收来自服务器的数据,并从扬声器中播放该数据。

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

声音接收器类:

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

1 个答案:

答案 0 :(得分:0)

看看我们现在检查的代码,我们想要发送数据的套接字是否与我们读取数据的套接字相同,如果是,我们跳过它。

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