Currently trying to have the server send a file to the client.
I'm able to see the file being created in the client directory, but it won't send all the bytes. Sometimes its 90% of the bytes, and sometimes its not even 10 bytes.
I keep getting a message from the terminal window saying: "malformed input around byte...."
Here's the Server code for this section:
String cMessage;
DataInputStream input= new DataInputStream( remote_socket.getInputStream() );
File filename = new File(cMessage);
try {
input = new DataInputStream(new FileInputStream(filename));
DataOutputStream out = new DataOutputStream(remote_socket.getOutputStream());
byte[] buffer = new byte[(int) filename.length()];
int count;
while((count = input.read(buffer)) >0){
out.write(buffer, 0, count);
}
System.out.println("Sending " + cMessage + " that is " + filename.length());
out.flush();
} catch (IOException e) {
System.out.println(e + "......");
e.printStackTrace();
}
Output for the Server when running looks like: Sending test.png that is 723915
Here's the Client for this section:
try {
BufferedReader reader= new BufferedReader(
new InputStreamReader(System.in) );
String message;
BufferedReader fileMessageReader= new BufferedReader(
new InputStreamReader(System.in) );
String fileMessage;
DataOutputStream output= new DataOutputStream(this.client_socket.getOutputStream() );
DataInputStream input = new DataInputStream(this.client_socket.getInputStream());
while(true) {
while( (message = reader.readLine()) != null ) {
System.out.println("1. For a text message. 2. For a file");
if (message.equals("2")) {
System.out.println("Name of file?");
fileMessage = fileMessageReader.readLine();
output.writeUTF(fileMessage);
try {
File filename = new File(fileMessage);
DataOutputStream fileOutput = new DataOutputStream(new FileOutputStream(filename));
byte[] buffer = new byte[1024];
int count;
while((count = input.read(buffer)) >0){
fileOutput.write(buffer, 0, count);
}
fileOutput.flush();
fileOutput.close();
} catch (IOException e) {
System.out.println(e + "...");
}
} else {
output.writeUTF( message );
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}