简体   繁体   English

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

[英]java.net.SocketException: Connection reset by peer: socket write error When transmitting Audio

Okay, so i am trying to make a voice chat program with java, and have been unsuccessful so far, because i keep getting this error: java.net.SocketException: Connection reset by peer: socket write error . 好的,我正在尝试使用Java制作语音聊天程序,但到目前为止仍未成功,因为我一直收到此错误: java.net.SocketException:连接被对等方重置:套接字写入错误

I have both classes for server and client(meaning it's only possible with 2 people), which makes the code same for sending and receiving audio: 我有服务器和客户端的类(这意味着只有两个人可以使用),这使得发送和接收音频的代码相同:

Server: 服务器:

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

Client: 客户:

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

I have tried transmitting the sound with both DataOutputStream and OutputStream, and i keep getting the same error for both client and server: 我尝试同时通过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)

This happens exactly when the sending takes place... I have googled that error, hoping that i will fix this, but have been less than fortunate. 这恰好发生在发送发生时...我用Google搜索了这个错误,希望我能解决这个问题,但是还不算很幸运。 All help is appreciated. 感谢所有帮助。

"Connection reset by peer" generally means that the remote system closed the connection that you tried to write to. “对等连接重置”通常意味着远程系统关闭了您尝试写入的连接。 This is not an issue with the type of stream objects you are using; 这与您使用的流对象的类型无关。 rather, it's a matter of what you do with them. 相反,这取决于您对他们的处理方式。 I'm not sure what that might be, because you have not presented a complete program. 我不确定这可能是什么,因为您尚未提交完整的程序。

Particularly notable by its absence is the client side of the code for establishing a connection. 由于缺少它,因此特别值得注意的是用于建立连接的代码的客户端。 I note, too, that the server (listening) side creates a new Thread around the ReceiveAudio object, but strangely doesn't start it. 我也注意到,服务器端(侦听)在ReceiveAudio对象周围创建了一个新的Thread ,但奇怪的是没有启动它。 Note: variable names should start with lowercase letters: receiveAudo . 注意:变量名称应以小写字母开头: receiveAudo

In any event, it is not helpful to use DataInputStream / DataOutputStream for this job, as these are targeted at transferring Java primitives and Objects. 无论如何,将DataInputStream / DataOutputStream用于此作业是无济于事的,因为它们旨在传输Java原语和对象。 Your data are raw as far as Java is concerned. 就Java而言,您的数据是原始的。 The stream implementations you obtain from the sockets will do everything you need to do. 从套接字获得的流实现将完成您需要做的所有事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.SocketException: Connection reset by peer: socket write error 提供文件时 - java.net.SocketException: Connection reset by peer: socket write error When serving a file (java.net.SocketException) 在处理请求时被捕获:连接被对等重置:套接字写入错误 - (java.net.SocketException) caught when processing request: Connection reset by peer: socket write error ClientAbortException:java.net.SocketException:由peer重置连接:套接字写入错误 - ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error “ java.net.SocketException:对等体重置连接:套接字写入错误”我该如何解决 - “java.net.SocketException: Connection reset by peer: socket write error” How can I solve it ClientAbortException:java.net.SocketException:对等重置连接:套接字写入错误 - ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error 我收到 java.net.SocketException: Connection reset by peer: socket write error ,当在类截图中执行主函数时 - I am getting java.net.SocketException: Connection reset by peer: socket write error ,when executing main function in class screenshot Influxdb-java:org.influxdb.InfluxDBIOException:java.net.SocketException:通过对等方重置连接:套接字写入错误 - influxdb-java: org.influxdb.InfluxDBIOException: java.net.SocketException: Connection reset by peer: socket write error java.net.socketexception connection reset by peer socket write error 通过Jenkins在Tomcat7上部署war时(使用Maven) - java.net.socketexception connection reset by peer socket write error While deploying war on Tomcat7 through Jenkins (Using Maven) 当发送沉重的json将近5MB时,它将给我java.net.SocketException:连接被同级重置:套接字写入错误 - While sending heavy json almost 5MB it will give me java.net.SocketException: Connection reset by peer: socket write error 错误java.net.SocketException:连接重置 - ERROR java.net.SocketException: Connection reset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM