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.
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:
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. All help is appreciated.