2 years ago
#60608

Sven Nijkamp
DataInputStream not getting the right value
I'm trying to setup a server-client file transfer where one user sends a file to the server, and the server sends that file to another user (users in this case are instances of the Client.java class).
From the first client to the server, everything goes as it should, but then the server should send a long, which is the file size, and then send the actual file. The client then should read that long as the file size, and then read and write the file, but with reading the file size something goes wrong.
Anyone that can spot what goes wrong?
Client class:
private void SEND() {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the full path of the file:");
String path = scanner.nextLine();
LIST();
System.out.println("To who do you want to send the file? ");
String receiver = scanner.nextLine();
message.setBody(receiver);
message.setHeader("SEND");
writer.println(message.getHeader() + " " + message.getUsername() + " " + message.getBody());
int bytes = 0;
File file = new File(path);
FileInputStream fileInputStream;
fileInputStream = new FileInputStream(file);
byte[] byteArray = new byte[(int) file.length()];
// send file size
dataOutputStream.writeLong(byteArray.length);
// break file into chunks
byte[] buffer = new byte[4 * 1024];
while ((bytes = fileInputStream.read(buffer)) != -1) {
dataOutputStream.write(buffer, 0, bytes);
dataOutputStream.flush();
}
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void RECEIVE() {
try {
long size = dataInputStream.readLong();
Scanner scanner = new Scanner(System.in);
char choice = scanner.next().charAt(0);
while (!(choice == 'y') && !(choice == 'n')) {
System.out.println("incorrect option, please enter y/n");
choice = scanner.next().charAt(0);
}
if (choice == 'y') {
System.out.println("Downloading file...");
int bytes = 0;
FileOutputStream fileOutputStream = new FileOutputStream(FILE_STORE_PLACE);
System.out.println(size);
byte[] buffer = new byte[4 * 1024];
while (size > 0 && (bytes = dataInputStream.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
fileOutputStream.write(buffer, 0, bytes);
size -= bytes; // read upto file size
}
System.out.println("File received");
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Server class:
// User sends their file to the server, which then reads al its contents and puts them in a file
public void SEND(String[] messageSplit) {
try {
int bytes = 0;
long size = dataInputStream.readLong(); // read file size
System.out.println(size);
byte[] buffer = new byte[4 * 1024];
ClientHandler receiver = findConnectedUser(messageSplit[2]);
promptReceiver(receiver, size);
receiver.dataOutputStream.writeLong(size);
receiver.dataOutputStream.flush();
//write incoming data to the receiver
while (size > 0 && (bytes = dataInputStream.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
receiver.dataOutputStream.write(buffer, 0, bytes);
size -= bytes; // read upto file size
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void promptReceiver(ClientHandler client, long size) {
client.writer.println("would like to send you a file, do you want to receive this file? (" + size + " bytes) (type y/n)");
}
public ClientHandler findConnectedUser(String username) {
for (ClientHandler client : connectedUsers) {
if (client.getUsername().equals(username)) {
return client;
}
}
return null;
}
java
sockets
datainputstream
dataoutputstream
0 Answers
Your Answer