I am transferring files over sockets from a client to a basic server. When I transfer small files (1kb) it fails on String fileName = clientData.readUTF(); but with large files (31mb) it works perfectly.
It seems to me it doesn’t have enough time. could anyone clarify this please?
tia
SERVER
public void receiveFile()
{
System.out.println("in receive file");
try {
int bytesRead;
DataInputStream clientData = new DataInputStream(clientSocket.getInputStream());
String fileName = clientData.readUTF();
OutputStream output = new FileOutputStream(("received_from_client_" + fileName));
long size = clientData.readLong();
byte[] buffer = new byte[1024];
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 " + clientID);
} catch (IOException ex) {
System.err.println("ERROR Connection closed Client " + clientID);
}
}
Trace
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at example3.CLIENTConnection.receiveFile(CLIENTConnection.java:75)
at example3.CLIENTConnection.run(CLIENTConnection.java:50)
at java.lang.Thread.run(Thread.java:722)
The peer has closed the connection. There is no more data. Close your end and forget about it.