[英]Filetransfer over Socket in Java, wont go out of while-loop
我正在嘗試創建一個簡單的服務器-客戶端程序,用戶可以在其中上載和下載文件。 我已經可以使用套接字和流了,並且可以將文件上傳到服務器。 但是,只要上傳了一個文件,服務器端就會陷入讀取流並將其轉發到服務器文件的循環中。
服務器代碼:
InputStream in = clientSocket.getInputStream();
String filePath = "......."
+ op[1];
System.out.println(op[0] + ": " + filePath);
FileOutputStream out = new FileOutputStream(filePath);
byte[] bytes = new byte[16*1024];
int count;
while ((count = in.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
客戶代碼:
String filePath = "...."
+ path;
System.out.println("Attempting: " + filePath);
dos = new DataOutputStream(serverSocket.getOutputStream());
fis = new FileInputStream(filePath);
byte[] buffer = new byte[4096];
while (fis.read(buffer) > 0) {
dos.write(buffer);
}
dos.flush();
fis.close();
問題在於程序陷入了while循環,因此服務器無法執行其他任何操作。 沒有錯誤或任何東西...
您永遠不會在客戶端關閉流。 在dos.close()
之后添加dos.close()
dos.flush()
!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.