i have a client-server program in java where the client sends a request to the server and the server responds by sending a file. The connection with the server is made correctly and the first request sent by the client is correctly interpreted by the server.
At this point the server reads in a specific file which seems to be read well since the write sends a correct number of bytes and a string (tested with a print of the number of bytes and of the string).
After sending the first message, the client waits to receive the file but as soon as it receives the message, the read returns -1 as if it had read an EOF and I can't understand the reason, how to solve it?
method on the server when to send the file
private void sendFile(Socket client, String path, DataOutputStream writer) {
File file = new File(path);
byte[] bytes = new byte[1024];
InputStream in = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int count;
try {
while ((count = in.read(bytes)) > 0) {
System.out.println("nBytes " + count);
String tmp = new String(bytes);
System.out.println(tmp);
//i'm reading from a json file this string
//[{"username":"user"},{"score":0},{"friendList":["us1","us2"]},{"password":"password"}]
//and it is printed correctly
writer.write(bytes, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
client-side method
public void getFile (String username) {
// first of all connects correctly with the server and sends a message, the message is received correctly by the server, after it waits for a reply message
Socket socket = connect2server();
DataInputStream reader = null;
BufferedWriter writer = null;
try {
reader = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//send message to the server using writer
OutputStream out = null;
String file = username+".json";
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] bytes = new byte[1024];
int count;
try {
while ((count = reader.read(bytes)) >= 0) {
System.out.println("nByte " + count);
out.write(bytes, 0, count);
}
// the client is waiting correctly on the read but as soon as it receives a message, the read returns -1 as if it had only read an EOF and not the whole message
System.out.println(count);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
out.close();
reader.close();
writer.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In your sendFile()
writer is not closed. You need to flush()
and then close()
the writer in order to guarantee the output is created. May be this will solve your problem