好的,我有一段代碼應該通過套接字發送多個文件。我在for循環中這樣做的方式我打開一個套接字 - >傳輸文件 - >關閉套接字,然後重複其他文件。上述代碼如下它:通過套接字傳輸時發生隨機錯誤
for (int i = 0; i < fname.size(); i++) {
Socket sok = new Socket("localhost",4444);
PrintStream oos = new PrintStream(sok.getOutputStream());
oos.println("1");
try {
System.out.println(fname.get(i));
File myFile = new File(path+fname.get(i));
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
// bis.read(mybytearray, 0, mybytearray.length);
DataInputStream dis = new DataInputStream(bis);
dis.readFully(mybytearray, 0, mybytearray.length);
OutputStream os = sok.getOutputStream();
// Sending file name and file size to the server
DataOutputStream doss = new DataOutputStream(os);
doss.writeUTF(myFile.getName());
doss.writeLong(mybytearray.length);
doss.write(mybytearray, 0, mybytearray.length);
doss.flush();
sok.close();
System.out.println("File " + fname.get(i) + " sent to Server.");
} catch (Exception e) {
System.err.println("File does not exist! (May not be true) Generated Error: "+e);
}
//sendFile(path+fname.get(i));
//sock.close();
}
} catch (Exception e) {
System.err.println(e);
}
現在發生的事情是客戶端沒有吐出任何錯誤,事實上,目前它實際上說,文件被髮送到服務器。現在服務器吐出了隨機錯誤。有時服務器會收到一些文件(從來都不是全部文件),有時它不會收到任何文件,而且出現的錯誤也是隨機的。錯誤是以下一項或其組合:
java.io.EOFException
java.io.FileNotFoundException
java.net.SocketException: Connection reset
java.io.UTFDataFormatException
所有這些錯誤都是在服務器端嘗試傳輸時發生的。我不知道這到底是怎麼回事=/
服務器代碼:
public void receiveFile() {
try {
int bytesRead;
DataInputStream clientData = new DataInputStream(
clientSocket.getInputStream());
String fileName = clientData.readUTF();
OutputStream output = new FileOutputStream((fileName));
long size = clientData.readLong();
byte[] buffer = new byte[100000];
while (size > 0
&& (bytesRead = clientData.read(buffer, 0,
(int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
output.close();
clientData.close();
System.out.println("File " + fileName + " received from client.");
} catch (IOException ex) {
System.err.println("Client error. Connection closed. " +ex);
}
}
堆棧跟蹤:
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at ClientConnection.receiveFile(ClientConnection.java:80)
at ClientConnection.run(ClientConnection.java:46)
at java.lang.Thread.run(Unknown Source)
1
Accepted connection : Socket[addr=/127.0.0.1,port=60653,localport=4444]
File LF-statistikkboka(Myers).pdf received from client.
1
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at ClientConnection.receiveFile(ClientConnection.java:80)
at ClientConnection.run(ClientConnection.java:46)
at java.lang.Thread.run(Unknown Source)
File WHAT IS LEFT TO DO.docx received from client.
按照要求的代碼,從客戶端接收命令
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String clientSelection;
clientSelection = in.readLine();
System.out.println(clientSelection);
while ((clientSelection) != null) {
switch (clientSelection) {
case "1":
receiveFile();
break;
case "2":
String outGoingFileName;
while ((outGoingFileName = in.readLine()) != null) {
sendFile(outGoingFileName);
}
break;
case "3":
sync();
break;
default:
System.out.println("Incorrect command received.");
break;
}
//in.close();
break;
}
請顯示服務器代碼 - 畢竟,這是錯誤的地方,對吧? –
完成。這就是接收文件並將錯誤吐出的功能。 –
哪些調用拋出異常? (什麼是堆棧跟蹤?) –