java.net.SocketException:通过对等方重置连接:套接字写入错误传输音频时

时间:2015-01-08 19:46:44

标签: java audio outputstream

好的,所以我正在尝试用java创建一个语音聊天程序,到目前为止一直没有成功,因为我不断收到此错误: java.net.SocketException:连接由peer重置:socket write error < /强>

我有服务器和客户端的两个类(意味着它只能用2个人),这使得代码在发送和接收音频方面都相同:

服务器:

    static AudioFormat format = new AudioFormat(44100,16,1,true,false);
    static TargetDataLine microphone;
    static SourceDataLine speakers;
    int errorcounter=0;
    byte[] targetData;
    public void AudioPreparation()
    {
        try{
            DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format);
            DataLine.Info sourceInfo = new DataLine.Info(SourceDataLine.class, format);
            microphone = (TargetDataLine) AudioSystem.getLine(targetInfo);
            speakers = (SourceDataLine) AudioSystem.getLine(sourceInfo);
            speakers.open(format);
            microphone.open(format);
            microphone.start();
            speakers.start();
            targetData = new byte[16];
        }
        catch (Exception e) {
            errorcounter++;
            if(errorcounter==5)
                System.exit(0);
            JOptionPane.showMessageDialog(null,e,"Error!",JOptionPane.ERROR_MESSAGE);
        }
    }
    final static int PORT = 6990;
    PrintWriter textout = null;
    BufferedReader textin = null;
    //DataOutputStream audioOut = null;
    OutputStream audioOut;
    //DataInputStream audioIn = null;
    InputStream audioIn;
    Thread RA = null;
    public void prepare()
    {
        AudioPreparation();

        while(true)
        {
            try {
                ServerSocket listener = new ServerSocket(PORT);
                Socket sock = listener.accept();
                textin = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                textout = new PrintWriter(sock.getOutputStream(), true);
                audioOut = sock.getOutputStream();
                audioIn = sock.getInputStream();
                //audioOut = new DataOutputStream(sock.getOutputStream());
                //audioIn = new DataInputStream(sock.getInputStream());                
                RA = new Thread(ReceiveAudio);
                break;
            } catch (IOException ex) {
                Logger.getLogger(ServerFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        while (true)
        {
            String name;
            try{
                textout.println("SUBMITNAME");
                name = textin.readLine();
            } catch (IOException ex) {
                return;
            }

            if(name==null) 
                return;
            else
            {
                Object[] question = {"Yes","Nope"};
                int n = JOptionPane.showOptionDialog(null,"Accept call from: "+name,"Incoming Call!",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,question,question[1]);
                if (n==1)
                {
                    return;
                }
                else
                {
                    textout.println("NAMEACCEPTED");
                    statuslabel.setText("Talk Session Active!");
                    RA.start();

                    while(true)
                    {
                        SendAudio();
                    }
                }

            }
        }
    }
    int numBytesRead;
    public void SendAudio()
    {
        numBytesRead = -1;
        try{
            numBytesRead = microphone.read(targetData, 0, targetData.length);
            if(numBytesRead>=0)
            {
                //speakers.write(targetData, 0, numBytesRead);
                audioOut.write(targetData, 0, targetData.length);    
                numBytesRead=-1;
                audioOut.flush();
            }
        }catch (Exception e){
            errorcounter++;
            if(errorcounter==5)
            {
                System.exit(0);
            }
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,e,"Error!",JOptionPane.ERROR_MESSAGE);
            numBytesRead = -1;
        }
    }

    Runnable ReceiveAudio = new Runnable()
    {
        public void run()
        {
            int bytesRead;
            while(true)
            {
                bytesRead = 0;
                try{
                    bytesRead = audioIn.read(targetData, 0, bytesRead);
                } catch (Exception e){
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null,e,"Error!",JOptionPane.ERROR_MESSAGE);
                    errorcounter++;
                    if(errorcounter==5)
                    {
                        System.exit(0);
                    }
                    return;
                }
                if(bytesRead >= 0)
                {
                    speakers.write(targetData, 0, bytesRead);
                    bytesRead=0;
                }
            }
        }
    };

客户端:

static AudioFormat format = new AudioFormat(44100,16,2,true,false);
static TargetDataLine microphone;
static SourceDataLine speakers;
int errorcounter=0;
public void AudioPreparation()
{
    try{

        DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format);
        DataLine.Info sourceInfo = new DataLine.Info(SourceDataLine.class, format);
        microphone = (TargetDataLine) AudioSystem.getLine(targetInfo);
        speakers = (SourceDataLine) AudioSystem.getLine(sourceInfo);
        speakers.open(format);
        microphone.open(format);
        microphone.start();
        speakers.start();
        targetData = new byte[16];
    }
    catch (Exception e) {
        errorcounter++;
        if(errorcounter==5)
            System.exit(0);
        JOptionPane.showMessageDialog(null,e,"Error!",JOptionPane.ERROR_MESSAGE);
    }
}
public static final int PORT = 6990;
Socket socket;
PrintWriter textout;
BufferedReader textin;
//DataOutputStream audioOut = null;
OutputStream audioOut;
//DataInputStream audioIn = null;
InputStream audioIn;
boolean connection = false;
byte[] targetData;
Thread RA;
public void connect()
{
    while(true)
    {
        String serverAddress = "localhost";
        try {
            socket = new Socket(serverAddress, PORT);
            textin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            textout = new PrintWriter(socket.getOutputStream(), true);
            audioOut = socket.getOutputStream();
            audioIn = socket.getInputStream();
            //audioOut = new DataOutputStream(sock.getOutputStream());
            //audioIn = new DataInputStream(sock.getInputStream());     
            RA = new Thread(ReceiveAudio);
            break;
        }catch(IOException e){
            JOptionPane.showMessageDialog(null,e,"Error!",JOptionPane.ERROR_MESSAGE);
            return;
        }
    }
    while (true) {
        try
        {
            String line = textin.readLine();
            if (line.startsWith("SUBMITNAME")) {
                textout.println(getName());
            }else if (line.startsWith("NAMEACCEPTED")) {
                statuslabel.setText("Talk Session Active!");
                RA.start();
                int numBytesRead;
                while(true)
                {
                    numBytesRead = -1;
                    try{
                        numBytesRead = microphone.read(targetData, 0, targetData.length);
                        if(numBytesRead>=0)
                        {
                            audioOut.write(targetData, 0, numBytesRead);    
                            numBytesRead=-1;
                            audioOut.flush();
                        }
                    }catch (Exception e){
                        errorcounter++;
                        if(errorcounter==5)
                        {
                            System.exit(0);
                        }
                        e.printStackTrace();
                        JOptionPane.showMessageDialog(null,e,"Error!",JOptionPane.ERROR_MESSAGE);
                        numBytesRead = -1;
                    }
                }

            }
        }catch(IOException e)
        {
            errorcounter++;
            JOptionPane.showMessageDialog(null,e,"Error!",JOptionPane.ERROR_MESSAGE);
            if(errorcounter==5)
            {
                break;
            }
        }
    }
}
public String getServerAddress() {
    String ipaddress = JOptionPane.showInputDialog(null,"Enter IP Address of the Server:","Chatter",JOptionPane.QUESTION_MESSAGE);
    return ipaddress;
}
Runnable ReceiveAudio = new Runnable()
{
    public void run()
    {
        int bytesRead = 0;
        while(true)
        {
            try{
                bytesRead = audioIn.read(targetData, 0, bytesRead);
            } catch (Exception e){
                bytesRead = 0;
                e.printStackTrace();
                JOptionPane.showMessageDialog(null,e,"Error!",JOptionPane.ERROR_MESSAGE);
                errorcounter++;
                if(errorcounter==5)
                {
                    System.exit(0);
                }
            }
            if(bytesRead > 0)
            {
                speakers.write(targetData, 0, bytesRead);
                bytesRead=0;
            }
        }
    }
};

String username = "";
public String getName() {
    while("".equals(username))
    {
        username =  JOptionPane.showInputDialog(null,"Choose a Username:","CryptoTalk",JOptionPane.PLAIN_MESSAGE);
    }
    return username;
}

我尝试使用DataOutputStream和OutputStream传输声音,并且我不断为客户端和服务器获取相同的错误:

java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)

这恰好发生在发送时...... 我用Google搜索了这个错误,希望我能解决这个问题,但一直不幸。 感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

“通过对等方重置连接”通常表示远程系统关闭了您尝试写入的连接。这不是您正在使用的流对象类型的问题;相反,这是你用它们做什么的问题。我不确定那可能是什么,因为你没有提出完整的程序。

特别值得注意的是它缺少的是用于建立连接的代码的客户端。我也注意到,服务器(监听)端在Thread对象周围创建了一个新的ReceiveAudio,但奇怪的是没有启动它。注意:变量名称应以小写字母开头:receiveAudo

无论如何,使用DataInputStream / DataOutputStream来完成这项工作是没有用的,因为它们的目标是传输Java基元和对象。就Java而言,您的数据是原始数据。您从套接字获得的流实现将完成您需要做的所有事情。