0

I am trying to write a program which uploads files from a client to a server. I think the issue I'm having at the moment is that the server doesn't wait for the client to start sending. Any pointers as to what is going wrong are greatly appreciated. The program has quite a lot in it including a GUI but I'll try and just include the relevant code.

Server

private InputStream in = null;
private OutputStream out = null;
private Socket socket;

private void startServer() {
    try {
        while (true) {
            ServerSocket server = new ServerSocket(port);
            System.out.println("Server started and listening on port " + port);
            socket = server.accept();
            System.out.println("Client connect from " + socket.getRemoteSocketAddress());
            in = new FileInputStream("File10.txt");
            out = socket.getOutputStream();

            while (in.read() >= 0) {
                receiveFile("File10.txt");
            }
        }
    } catch (Exception e) {
        System.out.println("File read failed");
    }

}

private void receiveFile(String name) {
    byte[] bytes = new byte[16 * 1024];
    File file = new File(name);
    int count = 0;

    try {
        while ((count = in.read(bytes)) > 0) {
            out.write(bytes, 0, count);
            System.out.println("File read");
        }
    } catch (Exception e) {
        System.out.println("Could not read file");
    }
}

Client

Queue<File> fileQueue = new ArrayDeque<File>();

private void startClient() {
    try {
        socket = new Socket(serverAddress, port);
        System.out.println("Connected to server at " + socket.getRemoteSocketAddress());

    } catch (Exception ex) {
        System.out.println("Could not connect to server");
    }
    try {
        br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        pw = new PrintWriter(socket.getOutputStream(), true);
        System.out.println("Reader/writer started");
    } catch (Exception e) {
        System.out.println("Reader/writer failed to start");
    }
}

private void sendFiles() {
    byte[] bytes = new byte[(int) fileQueue.peek().length()];
    int counter;

    try {
        fin = new FileInputStream(fileQueue.peek());
        bin = new BufferedInputStream(fin);
    } catch (Exception e) {
        System.out.println("Could not start Reader");
    }
    try {
        out = socket.getOutputStream();
        out.write(bytes, 0, bytes.length);
        System.out.println("Sending file ... ");
    } catch (Exception e) {
        System.out.println("Could not send file");
    }
 }

Ultimately the program will read files from a queue and upload them to the server. I'm only 8 weeks into learning Java and I might have been a bit ambitious with my choice of what to build.

Vermox
  • 57
  • 1
  • 1
  • 5
  • 1
    you are losing btyes here`while (in.read() >= 0) {` – Scary Wombat May 23 '17 at 06:54
  • Thanks. Should I be using a method other than read()? Or should I be comparing it to something else? – Vermox May 23 '17 at 06:57
  • You shouldn't be comparing anything to anything, or doing any I/O other than calling `receiveFile()`. NB This code will only read one file. If you need to read more than one, you need a completely different approach. – user207421 May 23 '17 at 07:05
  • Without the while loop I still have the issue that the server goes through `receiveFile()` without waiting for the client to send the file. Unless the issue is somewhere else that is. – Vermox May 23 '17 at 07:10
  • By completely different approach does that mean that I can't loop through sending files while there is a still a file in the queue and have the server loop through receiving the files the same amount of times? – Vermox May 23 '17 at 07:12
  • It is not possible for your server to get beyond `count = in.read(bytes)` until the client has sent something, and it is not possible for this read loop to terminate until the client closes its socket. By 'completely different approach' I mean the one in my answer in the duplicate. – user207421 May 23 '17 at 08:32

0 Answers0