2

TL;DR: How do I send (with a single connection) file, its size and its name. All examples in the internet send a file alone.

Server:

public class Server {
    private static int PORT = 6667;
    private ServerSocket serverSocket;

    public void run() throws IOException {
        System.out.println("Opening server");
        serverSocket = new ServerSocket(PORT);

        while(true) {
            try(Socket incomingSocket = serverSocket.accept()) {
                System.out.println("Accepted connection: " + incomingSocket);
                incomingSocket.setSoTimeout(2000); // Don't let scanner block the thread.

                InputStream inputStream = incomingSocket.getInputStream();
                Scanner scanner = new Scanner(inputStream);

                String command = "";
                if(scanner.hasNextLine())
                    command = scanner.nextLine();

                if(command.equals("update")) {
                    File file = new File("abc.txt");
                    sendFile(incomingSocket, file);
                }
                else {
                    // ...
                    System.out.println("Another command");
                }
            }
        }
    }

    private void sendFile(Socket socket, File file) throws IOException {
        byte[] bytes = new byte[(int)file.length()];
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        bufferedInputStream.read(bytes, 0, bytes.length);
        OutputStream outputStream = socket.getOutputStream();

        PrintWriter writer = new PrintWriter(outputStream, true);
        writer.println(file.length());
        writer.println(file.getName());

        System.out.println("Sending " + file.getName() + "(" + bytes.length + " bytes) to " + socket);
        outputStream.write(bytes, 0, bytes.length);
        outputStream.flush();
        System.out.println("File sent");
    }

    public void stopRunning() {
        try {
            serverSocket.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

Client:

public class Client {
    private static String HOST = "localhost";
    private static int PORT = 6667;

    public void run() throws IOException {
        Socket socket = new Socket(HOST, PORT);
        System.out.println("Connecting...");

        OutputStream outputStream = socket.getOutputStream();
        PrintWriter writer = new PrintWriter(outputStream, true);
        writer.println("update");      // Example command which will determine what server sends back

        receiveFile(socket);

        socket.close();
    }

    private void receiveFile(Socket socket) throws IOException {
        InputStream inputStream = socket.getInputStream();

        int size = 16384;
        String name = "example.txt";

        Scanner scanner = new Scanner(inputStream);
        size = Integer.parseInt(scanner.next());
        name = scanner.next();

        FileOutputStream fileOutputStream = new FileOutputStream(name);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        byte[] buffer = new byte[size];
        int bytesRead, totalRead = 0;
        while ((bytesRead = inputStream.read(buffer, 0, buffer.length)) != -1) {
            totalRead += bytesRead;
            bufferedOutputStream.write(buffer, 0, bytesRead);
        }
        bufferedOutputStream.flush();

        System.out.println("File " + name + " received. " + totalRead + " bytes read");

        bufferedOutputStream.close();
        fileOutputStream.close();
    }

I want my server to send a file to the client. It should also include the file's name and its size. Name because it's quite important and the size because I don't want to make a hardcoded buffer with a huge size.

Tried it with the code above. The client's "scanner part"

Scanner scanner = new Scanner(inputStream);
size = Integer.parseInt(scanner.next());
name = scanner.next();

works just okay, but the file is not received. inputStream.read(buffer, 0, buffer.length) never reads the remaining bytes from the stream.

If i comment out the scanner part, the bytes are read correctly(size and name information + file itself)

So, the question is, how do I send it with a single connection? Or should I make 2 separate connections, in the first one asking for size and file name and sending the file in the second one?

MrGrzyboo
  • 77
  • 8

1 Answers1

0

Scanner is good for text-based work.

One way to do what you want is using DataInputStream and DataOutputStream. Only one connection is needed:

public void send(File file, OutputStream os) throws IOException {
    DataOutputStream dos = new DataOutputStream(os);

    // writing name
    dos.writeUTF(file.getName());
    // writing length
    dos.writeLong(file.length());

    // writing file content
    ... your write loop, write to dos

    dos.flush();
}

public void receive(InputStream is) throws IOException {
    DataInputStream dis = new DataInputStream(is);

    String fileName = dis.readUTF();
    long fileSize = dis.readLong();

    // reading file content
    ... your read loop, read from dis
}
Roman Puchkovskiy
  • 11,415
  • 5
  • 36
  • 72