わかりましたので、ソケットを介して複数のファイルを送信することになっているコードのブロックがあります。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);
}
ここで何が起こるかというと、クライアントはエラーを吐き出さず、実際には、ファイルがサーバーに送信されたことを示しています。これで、サーバーはランダム エラーを吐き出します。サーバーがいくつかのファイルを受信する場合 (すべてではありません) もあれば、ファイルを受信しない場合もあり、発生するエラーもランダムです。エラーは、次の 1 つまたは組み合わせです。
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;
}