[英]Receiving multiple errors on the socket Java
我正在嘗試使用套接字創建文件傳輸系統。 在我開始從服務器向客戶端發送String fileName以使文件具有相同名稱之前,我的代碼曾經能夠正常工作。 現在,每當我嘗試發送文件時,它總是在客戶端和服務器中給我不同的錯誤。
服務器端代碼:
public void soc_server() throws IOException {
long time = System.currentTimeMillis();
long totalSent = 0;
ServerSocket servsock = null;
Socket sock = null;
PrintWriter pw = null;
FileInputStream fileInputStream = null;
try {
servsock = new ServerSocket(55000);
sock = servsock.accept();
System.out.println("Hello Server");
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the file name or file path");
String s = sc.nextLine();
sc.close();
File file = new File(s);
if (file.exists())
System.out.println("File found");
else
System.out.println("File not found");
OutputStream out = sock.getOutputStream();
pw = new PrintWriter(sock.getOutputStream(), true);
pw.print(s);
fileInputStream = new FileInputStream(s);
byte[] buffer = new byte[100 * 1024];
int bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
if (bytesRead > 0) {
out.write(buffer, 0, bytesRead);
totalSent += bytesRead;
System.out.println("sent " + (totalSent / 1024) + " KB "
+ ((System.currentTimeMillis() - time) / 1000)
+ " sec");
}
}
} catch (Exception e) {
System.out.println("exception " + e);
} finally {
sock.close();
pw.close();
servsock.close();
fileInputStream.close();
System.out.println("Sent " + (totalSent / 1024) + " kilobytes in "
+ ((System.currentTimeMillis() - time) / 1000) + " seconds");
}
}
客戶端代碼:
public void soc_client() throws Exception {
long time = System.currentTimeMillis();
long totalRecieved = 0;
Socket sock = null;
InputStream in = null;
BufferedReader br = null;
FileOutputStream fileOutputStream = null;
try {
sock = new Socket("172.16.27.106", 55000);
System.out.println("Hello Client");
in = sock.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
String fileName = br.readLine();
File outputFile = new File(fileName + "");
fileOutputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[100 * 1024];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
totalRecieved += bytesRead;
System.out.println("Recieved " + (totalRecieved / 1024)
+ " kilobytes in "
+ ((System.currentTimeMillis() - time) / 1000)
+ " seconds");
}
} catch (Exception e) {
System.out.println("Exception " + e);
} finally {
br.close(); // CLOSING BufferedReader
fileOutputStream.close();
sock.close();
System.out.println("Recieved " + totalRecieved + " bytes in "
+ (System.currentTimeMillis() - time) + "ms.");
}
}
例外情況:
客戶端:
Exception java.io.FileNotFoundException: Invalid file path
Exception: java.lang.NullPointerException
Exception in thread "main" java.io.FileNotFoundException: Invalid file path
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at Client.soc_client(Client.java:25)
at Index.main(Index.java:24)
服務器端:
Exception java.net.SocketException: Connection reset
Exception: java.util.NoSuchElementException
Exception java.net.SocketException: Broken pipe
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at Server.soc_server(Server.java:59)
at Index.main(Index.java:21)
我試圖發送的文件與我在其中編譯類的目錄(桌面)相同。 謝謝。
嘗試直接在客戶端路徑中提供文件名。
File outputFile = new File("yourfile.txt");
然后將其發送到服務器。
由於客戶端FileNotFound
,您將在finaly
塊中關閉流。
當您關閉客戶端流時,服務器端無法識別正在從中讀取的流,因此Connection reset
異常。
由於沒有流可以在服務器端讀取數據,因此您會收到NoSuchElement
異常
編輯
另一件事是,您不會在寫入客戶端后刷新流,
pw.flush();
在pw.print(s)
和out.write()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.