gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:09 25 4
gpt4 key购买 nike

好吧,我正在尝试用java制作一个语音聊天程序,但到目前为止一直不成功,因为我不断收到此错误:java.net.SocketException:对等方重置连接:套接字写入错误

我有服务器和客户端的类(这意味着只有 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)

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

最佳答案

“连接被对等方重置”通常意味着远程系统关闭了您尝试写入的连接。这不是您正在使用的流对象类型的问题;而是您正在使用的流对象的类型的问题。相反,问题在于你如何处理它们。我不确定那可能是什么,因为您还没有提供完整的程序。

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

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

关于java.net.SocketException : Connection reset by peer: socket write error When transmitting Audio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com