I am writing a program (in Objective C) to send a file of size 37 K across the Internet. I have written another program (in Java) to receive it that should run on the server. The trouble is, when the file arrives, the server program only receives a small part of it, not the entire file. Is the 37 K file being broken into smaller components?
public void run() {
try {
InputStream input = clientSocket.getInputStream();
OutputStream output = clientSocket.getOutputStream();
// Respond to input
long time = System.currentTimeMillis();
output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " +
this.serverText + " - " +
time +
"").getBytes());
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String now = sdf.format(cal.getTime());
mw.print("Date and time: " + now);
// read input
int num_bytes = input.available();
mw.print("Number of bytes received: " + num_bytes);
byte [] info = new byte[num_bytes];
for (int i = 0; i < num_bytes; i++)
{
info[i] = (byte) input.read();
}
// s contains all the info that we need
analyse_input(info, num_bytes);
output.close();
input.close();
} catch (IOException e) {
// report exception
mw.print("Problem receiving incoming file.");
e.printStackTrace();
}
}
input.available() might not give you total number of bytes which are in the stream.
rather use the following code