I have a server and client programs written up and working, but when I try to send a file though a bufferedinput and output streams the file is not sent. The file is created on the serve side but not populated by the info from the xml file on the client side. I dont see what I did wrong am I not flushing something write or using the wrong buffer streaming object?
here is the client:
import java.io.*; import java.net.*; /** * * @author Kenshin */ public class Client { public static void main(String[] args) { BufferedInputStream bis; BufferedOutputStream bos; int num; byte[] byteArray; File f = new File("file.xml"); Socket s = null ; try{ s = new Socket("localhost",8189) ; InputStream inStream = s.getInputStream() ; OutputStream outStream = s.getOutputStream() ; System.out.println("Connected to : " + s); BufferedReader inm = new BufferedReader(new InputStreamReader(inStream)); PrintWriter out = new PrintWriter(outStream, true /* autoFlush */); for (String x : args) { out.println(f); long len = f.length(); System.out.println(inm.readLine()); System.out.println("Sent File length = " + len); //SENDFILE bis = new BufferedInputStream(new FileInputStream(f)); bos = new BufferedOutputStream(s.getOutputStream( )); byteArray = new byte[8192]; while ((num = bis.read(byteArray)) != -1){ bos.write(byteArray,0,num); } bos.close(); bis.close(); System.out.println(inm.readLine()); } }catch (Exception e) { System.out.println(e) ; } } }
and here is the sever
import java.io.*; import java.net.*; /** * * @author Kenshin */ public class Serv { public static void main(String[] args) { BufferedInputStream bis; BufferedOutputStream bos; int num; try { int i = 1; ServerSocket s = new ServerSocket(8189); Socket incoming = s.accept(); System.out.println("Spawning " + i); try { try{ InputStream inStream = incoming.getInputStream(); OutputStream outStream = incoming.getOutputStream(); BufferedReader inm = new BufferedReader(new InputStreamReader(inStream)); PrintWriter out = new PrintWriter(outStream, true /* autoFlush */); out.println("File received "); //RECIEVE and WRITE FILE byte[] receivedData = new byte[8192]; bis = new BufferedInputStream(incoming.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream("file.xml")); //length = bis.read(receivedData); while ((num = bis.read(receivedData)) != -1){ bos.write(receivedData,0,num); } bos.close(); bis.close(); File receivedFile = new File("file.xml"); long receivedLen = receivedFile.length(); out.println("ACK: Length of received file = " + receivedLen); System.out.println("Length of received file = " + receivedLen); } finally { incoming.close(); } } catch (IOException e){ e.printStackTrace(); } } catch (IOException e1){ e1.printStackTrace(); } } }