I am having strange behavior with a client server image transfer. Currently the server always stalls when it has read 32768 bytes out of the buffer, the total file size is 36556 bytes. The while loop does not end, yet it doesn't print any of the print statements inside of it, and there are no exceptions thrown. I have tried several different size byte buffers and even going larger than the image size does not solve the issue.
Client Code:
private static void writePhoto(Socket socket) throws IOException {
OutputStream outputStream = new BufferedOutputStream(socket.getOutputStream());
String file_path = "E:\\Eclipse\\Workspace\\DNI_PsuedoClient\\" +
"src\\main\\resources\\BuckyBadger.jpg";
try {
InputStream stream = new FileInputStream(file_path);
System.out.println(stream.available());
try {
byte[] buffer = new byte[1024];
int readData;
int i = 1;
while((readData=stream.read(buffer))!=-1){
System.out.println(readData + " " + i);
outputStream.write(buffer,0,readData);
i++;
}
System.out.println("done writing to buffer");
}catch (IOException e) {
System.err.println("File Error");
e.printStackTrace();
System.exit(-1);
}finally {
stream.close();
}
} finally {
}
}
Server Code
private static java.io.File createFile(Socket client) throws IOException {
System.out.println("Starting to create file");
InputStream stream = new BufferedInputStream(client.getInputStream());
System.out.println("1");
// Create file from the inputStream
java.io.File Recieved_File = new java.io.File(thread_ID + ".jpg");
System.out.println(thread_ID + ".jpg");
System.out.println("2");
try {
OutputStream outputStream = new FileOutputStream(Recieved_File);
System.out.println("3");
try {
byte[] buffer = new byte[1024];
System.out.println("4");
int readData;
int i = 1;
while ((readData = stream.read(buffer)) != -1) {
System.out.println("start");
outputStream.write(buffer, 0, readData);
System.out.println(readData + " " + i + " " + (i * readData));
i++;
System.out.println("end while loop");
}
} finally {
System.out.println("5");
outputStream.close();
System.out.println("6");
}
} finally {
}
System.out.println("Created file");
return Recieved_File;
}
As you can see I have made several attempts using print statements in the Server code to try to figure out the issue. Any insight would be extremely helpful.
For further reference I have had the exact same server code working for an interaction with a Windows phone where the phone took a photo and uploaded it to the server. I am now changing the code to run on multiple threads and testing it in Java first with a java client that is simulating the interaction. The java client is attempting to send a photo stored on the drive to the server. The client appears to be working correctly as it puts the entire photo into the buffer.
I have written a protocol, here it is:
public DNI_Protocol(Socket socket, String threadID) {
client = socket;
thread_ID = threadID;
}
public String processInput(String theInput) {
String theOutput = null;
if (state == WAITING) {
System.out.println(theInput);
if (theInput.equals("Upload")) {
state = UPLOAD_PHOTO;
theOutput = "Send Photo";
} else if (theInput == "View") {
state = VIEW;
theOutput = "Send Request";
} else {
theOutput = "Waiting";
}
} else if (state == UPLOAD_PHOTO) {
// if (theInput != "Sending Photo") {
// TODO: Throw a state exception with a message
// return "exit";
// }
setupDrive();
try {
Image_Handle = createFile(client);
System.out.println("Uploading file");
uploadedFile = Drive_Interface.uploadFile(false, Image_Handle, drive);
System.out.println("file Uploaded");
google_ID = uploadedFile.getId();
System.out.println(google_ID);
Image_Handle.delete(); // We are done with the file so we can delete it
System.out.println("deleted file");
} catch (IOException e) {
e.printStackTrace();
}
theOutput = "Send Keywords";
state = UPLOAD_KEYWORDS;
} else if (state == UPLOAD_KEYWORDS) {
if (theInput != "Sending Keywords") {
// TODO: Throw a state exception with a message
return "exit";
}
// TODO: Add code to get keyword arraylist and upload information to
// the database
theOutput = "exit";
}
return theOutput;
}