我對客戶機服務器映像傳輸有奇怪的行爲。目前,當服務器從緩衝區中讀取32768個字節時總是停頓,總文件大小爲36556字節。 while循環不會結束,但它不會打印其中的任何打印語句,也不會拋出異常。我已經嘗試了幾種不同大小的字節緩衝區,甚至比圖像大小還要大,並不能解決問題。通過TCP連接傳輸圖像時遇到問題
客戶端代碼:
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 {
}
}
服務器代碼
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;
}
正如你可以看到我已經使用打印語句中的服務器代碼,試圖找出問題的幾點嘗試。任何見解都會非常有幫助。
爲了進一步的參考,我已經有了完全相同的服務器代碼,用於與Windows手機進行交互,手機在該手機上拍照並將其上傳到服務器。我現在將代碼更改爲在多個線程上運行,並使用模擬交互的Java客戶端首先在Java中進行測試。 Java客戶端正試圖將存儲在驅動器上的照片發送到服務器。客戶端看起來正常工作,因爲它將整張照片放入緩衝區。
我寫了一個協議,那就是:
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;
}
你期待什麼發生?您的代碼無法讓客戶端停止服務器繼續等待更多數據。所以服務器一直在等待。 (當您設計用於在這兩個程序之間進行通信的協議時,您應該回答的兩個問題是「客戶端如何告訴服務器它具有整個文件?」以及「服務器如何知道它具有整個文件?「)也許你打算關閉客戶端的連接? –
你是否曾嘗試調用'flush()',使用'Buffered'相關流的所有實例?由於我對「文件處理」不太熟悉,因此我只能提供一個小想法來看看。 –
@GagandeepBali沒有明確的緩衝,所以唯一的緩衝是Nagle的算法,它不會引入很長的延遲。 –