0

My project is to write a server and client class. The server waits to receive a path for a file which it will convert from CSV to XML and then it's supposed to read the contents of the file back to the client who will save the content into a new file. I've gotten 90% of it done so far, the only thing is that the file in the end comes out empty, but on the server side the XML file is created.

This is the servers connection handling method

public void handleConnection(InputStream sockInput, OutputStream sockOutput){
    CsvToXml csv = new CsvToXml();
    String csvAddress;

    while(true)
    {
        byte[] buffer = new byte[4094];
        int bytes_read = 0;
        byte[] bytes_out = new byte[4094];

        try {
            bytes_read = sockInput.read(buffer, 0, buffer.length);
            csvAddress = new String(buffer, 0, bytes_read);
            System.err.println(csvAddress);
            csv.convertFile(csvAddress , "C:/Users/Arian/Desktop/xmlFile.xml", ",");

            if(bytes_read < 0){
                System.err.println("Tried to read from socket, but returned < 0. Closing socket.");
                return;
            }


            System.out.println("Server Received "+ bytes_read + "bytes, data=" + (new String(buffer, 0, bytes_read)));


            bytes_out = readXml();
            bytes_read = sockInput.read(bytes_out, 0, bytes_out.length);
            sockOutput.write(bytes_out);

            sockOutput.flush();
        } catch(Exception e){
            System.err.println("Exception reading/writing from/to socket, e=" +e);
            e.printStackTrace(System.err);
            return;
        }
    }
}

So this part I read and put the xml file into a byte array and return it.

public byte[] readXml(){

    File file = new File("C:/Users/Arian/Desktop/xmlFile.xml");
    byte[] xmlFile = new byte[(int)file.length()];
    FileInputStream fileInputStream = null;


    try {
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(xmlFile);
        fileInputStream.close();
    } catch (Exception e) {
        System.err.println("File not found or unreadable.");
        e.printStackTrace();
    }

    return xmlFile;
}

and this is the clients inputStream and outputStream method

public void startConversion(int iterations) {

    System.err.println("Opening connection to "+serverHostname+" port "+serverPort);
    try {
        sock = new Socket(serverHostname, serverPort);
        sockInput = sock.getInputStream();
        sockOutput = sock.getOutputStream();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        return;
    }

    System.err.println("About to start reading/rwriting to/from socket");   

    for(int i = 1; i <= iterations; i++){
        try{
            sockOutput.write(data, 0, data.length);
            File file = new File("C:/Users/Arian/Desktop/NewFile.xml");
            byte[] buffer = new byte[sockInput.available()];
            sockOutput = new FileOutputStream(file);
            int len;
            sockInput.read(buffer);
            sockOutput.write(buffer);
            sockOutput.flush();


        } catch(IOException e){
            e.printStackTrace(System.err);
        }






    System.err.println("Done reading/writing to/from socket, closing socket.");

    try {
        sock.close();
    } catch (IOException e){
        System.err.println("Exception closing socket.");
        e.printStackTrace(System.err);
    }

    System.err.println("Exiting.");
    }



}

I've tried many methods i've looked up on other posts and just haven't had any success with any of them. I cant post more than two links so heres my server and client class, my conversion class is fine anyway.

http://pastebin.com/C34hVNEW - Client Class http://pastebin.com/CF6BJTMP - Server Class

UPDATE:

I changed the for loop inside my client class to this and it still doesn't work. I put a comment where the client stops debugging.

    for(int i = 1; i <= iterations; i++){
        try{
            sockOutput.write(data, 0, data.length);
            File file = new File("C:/Users/Arian/Desktop/NewFile.xml");
            byte[] buffer = new byte[8192];
            sockOutput = new FileOutputStream(file);
            int count;
            while((count = sockInput.read(buffer)) > 0){ //The client stops debugging here.
                sockOutput.write(buffer, 0, count); 
            }
            sockOutput.flush();


        } catch(IOException e){
            e.printStackTrace(System.err);
        }
Arian.No
  • 3
  • 1
  • 5
  • A lot of code. You say you get to 90% of it, if you debug/step through your code while executing it, where does it go wrong? If it's only at the writing to file at the end, then it got to be a trivial problem. – mattias Nov 08 '14 at 23:26
  • When I debug the client it doesn't do anything at all. If i put a break point right before the for loop that handles the input and output streams then it just immediately jumps right to loop and stops on the break point... – Arian.No Nov 09 '14 at 09:23
  • It is supposed to stop at the break point, that is why it is called break point. After that, you yourself need to make the flow of your program continue. In for example Eclipse this would normally be done with the buttons F5-F8 for different actions. – mattias Nov 09 '14 at 09:28
  • Yeah I continued and it stops at the while loop I just added in. I wrote exactly where in my update if you look at it. – Arian.No Nov 09 '14 at 09:41

1 Answers1

1

You're making a real meal out of this. The standard way to copy between streams in Java is as follows:

int count;
byte[] buffer = new byte[8192]; // or whatever you like, any size greater than zero
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}

Note that you have to loop; that you don't need a buffer the size of the input; and that you have to use the read count when wrting, as read() isn't obliged to fill the buffer, and almost certainly won't on at least the last read before EOS. You can use this same code at both ends, by suitable adjustment of in and out.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • So I changed the bit you mentioned, I actually previously had it set up like that as well and thats why I have the random "int len". I had that instead of count for pretty much the same piece of code, and it still doesn't work. I wrote an update on where it went wrong in the debug. – Arian.No Nov 09 '14 at 09:27