Question: XML file communication between server and client

Discussion in 'Java' started by Fengyuan, Apr 8, 2007.

  1. Fengyuan

    Fengyuan Guest

    Hi, All

    I have created a random array of data and transferred them to XML file
    as my random noise file. Now, I would like to transfer the noise from
    my client to the server through TCP socket connection. I referred to
    the KnockKnock example in the tutorial on java.sun.com.

    Here is the code of the my client, I don't know how to modify the part
    I noted by //----------- so that I can transfer my noise.xml file.

    public class Client {
    public static void main(String[] args) throws IOException {

    Socket serverSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    // Set up the connect to server
    try {
    serverSocket = new Socket("ServerName", 123);
    //server name and port number on the server
    //client's socket is bound to any available local port
    out = new PrintWriter(serverSocket.getOutputStream(),
    true);
    in = new BufferedReader(new
    InputStreamReader(serverSocket.getInputStream()));
    } catch (UnknownHostException e) {
    System.err.println("Don't know about host: ServerName.");
    System.exit(1);
    } catch (IOException e) {
    System.err.println("Couldn't get I/O for the connection
    to: ServerName.");
    System.exit(1);
    }

    //Communication between client and server
    //??????????????????????????????????????
    //How to implement my experiment: Write the rand number in XML
    file I generated to server?

    //----------------------------------------------------------------------------
    BufferedReader stdIn = new BufferedReader(new
    InputStreamReader(System.in));
    String fromServer;
    String fromUser;

    while ((fromServer = in.readLine()) != null) {
    System.out.println("Server: " + fromServer);
    if (fromServer.equals("Bye."))
    break;

    fromUser = stdIn.readLine();
    if (fromUser != null) {
    System.out.println("Client: " + fromUser);
    out.println(fromUser);
    //-----------------------------------------------------------------------------
    }
    }

    //Clean all the ports
    out.close();
    in.close();
    stdIn.close();
    serverSocket.close();
    }
    }

    I help someone can give me some idea and help me out. Thank you.

    Fengyuan (Thomas)
     
    Fengyuan, Apr 8, 2007
    #1
    1. Advertising

  2. Fengyuan

    SadRed Guest

    On Apr 8, 2:09 pm, "Fengyuan" <> wrote:
    > Hi, All
    >
    > I have created a random array of data and transferred them to XML file
    > as my random noise file. Now, I would like to transfer the noise from
    > my client to the server through TCP socket connection. I referred to
    > the KnockKnock example in the tutorial on java.sun.com.
    >
    > Here is the code of the my client, I don't know how to modify the part
    > I noted by //----------- so that I can transfer my noise.xml file.
    >
    > public class Client {
    > public static void main(String[] args) throws IOException {
    >
    > Socket serverSocket = null;
    > PrintWriter out = null;
    > BufferedReader in = null;
    > // Set up the connect to server
    > try {
    > serverSocket = new Socket("ServerName", 123);
    > //server name and port number on the server
    > //client's socket is bound to any available local port
    > out = new PrintWriter(serverSocket.getOutputStream(),
    > true);
    > in = new BufferedReader(new
    > InputStreamReader(serverSocket.getInputStream()));
    > } catch (UnknownHostException e) {
    > System.err.println("Don't know about host: ServerName.");
    > System.exit(1);
    > } catch (IOException e) {
    > System.err.println("Couldn't get I/O for the connection
    > to: ServerName.");
    > System.exit(1);
    > }
    >
    > //Communication between client and server
    > //??????????????????????????????????????
    > //How to implement my experiment: Write the rand number in XML
    > file I generated to server?
    >
    > //----------------------------------------------------------------------------
    > BufferedReader stdIn = new BufferedReader(new
    > InputStreamReader(System.in));
    > String fromServer;
    > String fromUser;
    >
    > while ((fromServer = in.readLine()) != null) {
    > System.out.println("Server: " + fromServer);
    > if (fromServer.equals("Bye."))
    > break;
    >
    > fromUser = stdIn.readLine();
    > if (fromUser != null) {
    > System.out.println("Client: " + fromUser);
    > out.println(fromUser);
    > //-----------------------------------------------------------------------------
    > }
    > }
    >
    > //Clean all the ports
    > out.close();
    > in.close();
    > stdIn.close();
    > serverSocket.close();
    > }
    >
    > }
    >
    > I help someone can give me some idea and help me out. Thank you.
    >
    > Fengyuan (Thomas)


    If the only expected functionality of your client is
    sending an XML file to the server. The 'in' and 'stdIn'
    in your current code are useless, aren't they?
    (1)Read each line from your XML file
    (2)Send the line to the server in a while loop.
     
    SadRed, Apr 8, 2007
    #2
    1. Advertising

  3. Fengyuan

    Fengyuan Guest

    On Apr 8, 1:30 am, "SadRed" <> wrote:
    > On Apr 8, 2:09 pm, "Fengyuan" <> wrote:
    >
    >
    >
    > > Hi, All

    >
    > > I have created a random array of data and transferred them to XML file
    > > as my random noise file. Now, I would like to transfer the noise from
    > > my client to the server through TCP socket connection. I referred to
    > > the KnockKnock example in the tutorial on java.sun.com.

    >
    > > Here is the code of the my client, I don't know how to modify the part
    > > I noted by //----------- so that I can transfer my noise.xml file.

    >
    > > public class Client {
    > > public static void main(String[] args) throws IOException {

    >
    > > Socket serverSocket = null;
    > > PrintWriter out = null;
    > > BufferedReader in = null;
    > > // Set up the connect to server
    > > try {
    > > serverSocket = new Socket("ServerName", 123);
    > > //server name and port number on the server
    > > //client's socket is bound to any available local port
    > > out = new PrintWriter(serverSocket.getOutputStream(),
    > > true);
    > > in = new BufferedReader(new
    > > InputStreamReader(serverSocket.getInputStream()));
    > > } catch (UnknownHostException e) {
    > > System.err.println("Don't know about host: ServerName.");
    > > System.exit(1);
    > > } catch (IOException e) {
    > > System.err.println("Couldn't get I/O for the connection
    > > to: ServerName.");
    > > System.exit(1);
    > > }

    >
    > > //Communication between client and server
    > > //??????????????????????????????????????
    > > //How to implement my experiment: Write the rand number in XML
    > > file I generated to server?

    >
    > > //----------------------------------------------------------------------------
    > > BufferedReader stdIn = new BufferedReader(new
    > > InputStreamReader(System.in));
    > > String fromServer;
    > > String fromUser;

    >
    > > while ((fromServer = in.readLine()) != null) {
    > > System.out.println("Server: " + fromServer);
    > > if (fromServer.equals("Bye."))
    > > break;

    >
    > > fromUser = stdIn.readLine();
    > > if (fromUser != null) {
    > > System.out.println("Client: " + fromUser);
    > > out.println(fromUser);
    > > //-----------------------------------------------------------------------------
    > > }
    > > }

    >
    > > //Clean all the ports
    > > out.close();
    > > in.close();
    > > stdIn.close();
    > > serverSocket.close();
    > > }

    >
    > > }

    >
    > > I help someone can give me some idea and help me out. Thank you.

    >
    > > Fengyuan (Thomas)

    >
    > If the only expected functionality of your client is
    > sending an XML file to the server. The 'in' and 'stdIn'
    > in your current code are useless, aren't they?
    > (1)Read each line from your XML file
    > (2)Send the line to the server in a while loop.


    Thank you for the reply. How can I read each line from my XML file
    into the client and then send to the server? Do I need XML parser at
    client side or the parser at server side? I am new to Java, hopefully
    someone can help me out. Thanks

    Fengyuan (Thomas)
     
    Fengyuan, Apr 8, 2007
    #3
  4. Fengyuan wrote:
    > Thank you for the reply. How can I read each line from my XML file
    > into the client and then send to the server? Do I need XML parser at
    > client side or the parser at server side? I am new to Java, hopefully
    > someone can help me out. Thanks


    To read a line from a file, use something like this:

    BufferedReader fin = new BufferedReader(new FileReader("filename.xml"));
    String line = fin.readLine();

    To write this line to the server, use:
    out.println(line);

    The easiest way to send an entire file this way is to use this loop:
    BufferedReader fin = new BufferedReader(new FileReader("filename.xml"));
    String line;
    while ((line = fin.readLine()) != null) {
    out.println(line);
    }

    If you want to parse the XML file, then yes, you will need to use an XML
    parser at the server side (but not the client).

    For further information, refer to the javadocs at
    http://java.sun.com/j2se/1.5.0/docs/api/index.html or
    http://java.sun.com/j2se/1.4.2/docs/api/index.html depending on your
    version.
     
    Joshua Cranmer, Apr 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. szymex
    Replies:
    1
    Views:
    422
    Michael Borgwardt
    May 3, 2004
  2. Michelle
    Replies:
    2
    Views:
    428
    Michelle
    Jul 19, 2004
  3. Raj
    Replies:
    1
    Views:
    499
    Andy Dingley
    Oct 20, 2004
  4. Stefka
    Replies:
    4
    Views:
    602
    Stefka
    Jul 6, 2006
  5. Guy Cohen

    Communication between client and server

    Guy Cohen, Oct 1, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    383
    John Timney \(MVP\)
    Oct 2, 2006
Loading...

Share This Page