Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Sending xml file over a socket

  1. #1
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Sending xml file over a socket

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


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Sending xml file over a socket

    When I compile this, the contents of file.xml is deleted from the client side and then an exception is thrown.
    I changed the file name to file2.xml on the Serv class but it doesn't get as far as creating the file.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    Kakashi (March 9th, 2011)

  4. #3
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Sending xml file over a socket

    I got it now, I used a BufferedReader and read the file into that then send that to printwriter to the server and that worked. Yeah
    I really dont know why the file was being deleated it was bothering me a bit. I even created a file and put it on the server side and then it gave me that the connectinon was refused. Pick server I guse.
    Thanks for the help

Similar Threads

  1. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  2. Sending object through socket
    By Alexandrinne in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 16th, 2010, 02:18 AM
  3. Monitor the progress of sending a file to client
    By adumi in forum Java Theory & Questions
    Replies: 0
    Last Post: April 17th, 2010, 07:01 AM
  4. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 09:10 AM
  5. sending an email with pdf file everyday
    By pradeepptp in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: June 21st, 2009, 10:49 AM