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);
}