0

So I am learning about basic TCP/IP applications and I am trying to figure out a way to send text files one after another. The first line that the server reads will be the file name and then the data in the file will be read after. So I'm trying to explore ways to read until the end of the first file and then start another stream to write another file. My application freezes after the second try. So I dont know if the problem is on the client side or the server side, and I am not sure how to solve this.

I have methods for getting the connection, getting streams, closing connection, and processing connection. I am just posting the processing connection method for the server and client.

CLIENT:

private void processConnection() throws Exception
    {   //read file
        Path filePath = null;
        try
        {
            filePath = Paths.get(inputpath);
            fileToSend = Files.newInputStream(filePath);
            reader = new BufferedReader(new InputStreamReader(fileToSend));
            //send file name
            output.writeObject(path);
            output.flush();

            //send file data
            String linesToSend = reader.readLine();
            while(linesToSend != null)
            {
                output.writeObject(linesToSend);
                output.flush();
                linesToSend = reader.readLine();
            }
            message = (String)input.readObject();
            display.append("\n" + message);
        }
        catch(NullPointerException e)
        {
            display.append("\nNo file to send.");
        }
    }

SERVER:

private void processConnection() throws IOException
        {
            String message = "Connection successful";
            sendData(message);
                try
                {   
                    //read first line of input for file name
                    String clientInput = (String) input.readObject();
                    //create file path
                    String abFilePath = pathToString + "\\" + clientInput;
                    // create file
                    FileSystem fs = FileSystems.getDefault();
                    Path path = fs.getPath(abFilePath);
                    fileOutput = new BufferedOutputStream(Files.newOutputStream(path, CREATE_NEW));
                    writer = new BufferedWriter(new OutputStreamWriter(fileOutput));
                    //read rest of data
                    String s = (String)input.readObject();
                    while(s != null)
                    {
                        writer.write(s, 0, s.length());
                        s = (String)input.readObject();
                    }
                    writer.close();
                    display.append("\nFile " + clientInput + " was recieved.");
                    message = "\n" + clientInput + " was sent to server.";
                    sendData(message);
                }
                catch(ClassNotFoundException | SocketException e)
                {
                    e.printStackTrace();
                    display.append("\nClient disconnected\n");
                    input = null;
                }
                catch(FileAlreadyExistsException e)
                {
                    display.append("\nFile already exist.");
                }
            }
JavaFox
  • 61
  • 14
  • Are you creating a new connection for the second send? Object streams are often a bad choice, because they seem attractive for the more beginners, but have a lot of caveats and are rarely the right choice for communication. – Kayaman Nov 01 '15 at 21:28
  • No I am not, my getStreams method gets a new stream from the socket. I was thinking that is what I should do but not sure if I must close the first one. Sockets are really complicated for beginners such as myself and Object streams are the easiest for me to learn on. I guess sending bytes would probably be best. – JavaFox Nov 01 '15 at 21:34
  • It's never a good idea to get streams from a `Socket` more than once. – Kayaman Nov 01 '15 at 21:36
  • So am I to close the first stream, and get a new one? OR just get a new stream. I need the connection to stay open – JavaFox Nov 01 '15 at 21:40
  • `ObjectOutputStream` writes headers. If you create a new `OOS` it will send its own headers. If the server is reading from a single `ObjectInputStream` it expects one set of headers, not multiple. Therefore you'd need to keep the `ObjectOutputStream` open always, never get new streams or close the old one. I recommend a byte based approach. – Kayaman Nov 02 '15 at 11:09
  • I figured it out. I just send a string to the server that indicates the file data has ended and then test for that string on the server side(replace the boolean in the loop) thus exiting the loop and knowing when the file has finished ^^. Then add a while loop surrounding the server code while socket.isBounded(). Doing this with bytes would be more difficult but more efficient. Luckily that question has already been answered :3 – JavaFox Nov 03 '15 at 03:59

0 Answers0