I am having difficulty with my chat application in Java that I am creating in context of being able to send multiple files.. Example; I send my.txt which is successful then I go to send hello.txt which is not successful... first time always works but second third etc. does not.. I do not understand what is going wrong here this is my first app with Sockets so still kind of a rookie at it.. Below are the two methods essentially handling the executions of send/receive if you need to see the main class in context of the while true loop let me know and ill happily post anything thats required, thanks everyone for your help in advance, excited to get this thing going and learn!
public void sendSound() throws IOException {
connection2 = new Socket(InetAddress.getByName(serverIP), 1990);
System.out.println("send voice");
OutputStream output2 = new ObjectOutputStream(connection2.getOutputStream());
output2.flush();
InputStream input2 = new ObjectInputStream(connection2.getInputStream());
System.out.println(input2);
DataOutputStream dos = null;
try {
dos = new DataOutputStream(connection2.getOutputStream());
} catch (IOException ex1) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
System.out.println("error creating data input stream ");
}
FileInputStream fis = null;
try {
fis = new FileInputStream(voiceOutput);
} catch (FileNotFoundException ex1) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
System.out.println("error in creating file input stream");
}
int count = 0;
byte[] buffer = new byte[4096];
try {
while ((count = fis.read(buffer)) > 0) {
dos.write(buffer, 0, count);
dos.flush();
System.out.println(fis);
}
} catch (IOException ex1) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
System.out.println("error in loop writing");
}
try {
fis.close();
} catch (IOException ex1) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
System.out.println("error closing fis");
}
try {
dos.flush();
dos.close();
} catch (IOException ex1) {
Logger.getLogger(EchoRun.class.getName()).log(Level.SEVERE, null, ex1);
System.out.println("or closing dos");
}
}
}
class downloadVoiceFile implements Runnable {
/*
Play audio clip VARS
*/
private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
AudioSystem clip;
Echo echo;
ServerSocket server_download;
Socket connection_download;
FileOutputStream fos;
DataInputStream dis;
OutputStream output;
InputStream input;
@Override
public void run() {
System.out.println("downloading file.........");
try {
server_download = new ServerSocket(1990, 100);
} catch (IOException ex) {
Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
}
try {
connection_download = server_download.accept();
} catch (IOException ex) {
Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
}
try {
output = new ObjectOutputStream(connection_download.getOutputStream());
output.flush();
} catch (IOException ex) {
Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
}
try {
input = new ObjectInputStream(connection_download.getInputStream());
} catch (IOException ex) {
Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
}
try {
dis = new DataInputStream(connection_download.getInputStream());
} catch (IOException ex) {
Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream("voiceOut.wav");
} catch (FileNotFoundException ex) {
Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] buffer = new byte[4096];
System.out.println(buffer.length);
int read = 0;
try {
while ((read = dis.read(buffer)) > 0) {
fos.write(buffer, 0, read);
}
} catch (IOException ex) {
Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
}
try {
fos.close();
dis.close();
output.close();
input.close();
playStart();
int reply = JOptionPane.showConfirmDialog(null, "You Have Received A Voice Messsage", "Voice Clip Received",
JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
System.out.println("yes");
playVoiceFile();
} else {
System.out.println("no");
File f = new File("voiceOut.wav");
boolean bool = false;
bool = f.delete();
}
} catch (IOException ex) {
Logger.getLogger(downloadVoiceFile.class.getName()).log(Level.SEVERE, null, ex);
}
}

New Topic/Question
Reply


MultiQuote


|