Im getting corrupted data over my Socket I don't really understand what the problem is. Im sending a voice clip that the user records when they let go it activated this method below in which it sends. when I look at the file sent its always a lot smaller than the original file I recorded.
UPDATED 22:00/23/03 **SENDING END
public void sendVoice() throws IOException{
System.out.println("send voice");
Socket connection2 = new Socket(InetAddress.getByName("127.0.0.1"),1200);
System.out.println(connection2);
ObjectOutputStream output2 = new ObjectOutputStream(connection2.getOutputStream());
output2.flush();
ObjectInputStream input2 = new ObjectInputStream(connection2.getInputStream());
System.out.println(input2);
DataOutputStream dos = null;
try {
dos = new DataOutputStream(connection2.getOutputStream());
} catch (IOException ex1) {
Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
}
FileInputStream fis = null;
try {
fis = new FileInputStream(voiceOutput);
} catch (FileNotFoundException ex1) {
Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
}
// new
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(chat.class.getName()).log(Level.SEVERE, null, ex1);
}
try {
fis.close();
} catch (IOException ex1) {
Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
}
try {
dos.flush();
dos.close();
////
} catch (IOException ex1) {
Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
}
}
RECEIVING END IS
public void downloadFile() throws IOException {
server2 = new ServerSocket(1200,100);
System.out.println("downloading file.........");
Socket connection2 = new Socket();
connection2 = server2.accept();
ObjectOutputStream output2 = new ObjectOutputStream(connection2.getOutputStream());
output2.flush();
ObjectInputStream input2 = new ObjectInputStream(connection2.getInputStream());
DataInputStream dis = new DataInputStream(connection2.getInputStream());
FileOutputStream fos = new FileOutputStream("voiceClip.wav");
byte[] buffer = new byte[4096];
System.out.println(buffer.length);
//int filesize = 15123;
int read = 0;
//int remaining = filesize;
while ((read = dis.read(buffer)) > 0)
{
fos.write(buffer, 0, read);
}
/*
while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
*/
fos.close();
dis.close();
}