Results 1 to 4 of 4
  1. #1
    colossusdub is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Saving data from a socket to a file in real time

    Hi,

    I am using a tracking system which sends x, y and z coordinates over a socket. I want to store this information in a file to hopefully read back in later as seperate pieces of information, i.e the x coordinate, the y coordinate, the z coordinate. The x, y and z coordinates are updated every second so i need the file to update accordingly. So far I can get the information printing in my java shell but I am struggling with getting the information to print to a file. Any help would be greatly appreciated.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    
    public class File {
        public static void main(String[] args) throws IOException {
    
            Socket kkSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;
    
            try {
                kkSocket = new Socket("192.168.33.39", 12000);
                out = new PrintWriter(kkSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: Adam.");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: Adam.");
                System.exit(1);
            }
    
            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;
    
     try{
        // Create file 
        FileWriter fstream = new FileWriter("outs.txt");
            BufferedWriter outs = new BufferedWriter(fstream);
        outs.write("Server: " + fromServer);
        //Close the output stream
        outs.close();
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
    		
    
            }
    
            out.close();
            in.close();
            stdIn.close();
            kkSocket.close();
        }
    }

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Quote Originally Posted by colossusdub View Post
    The x, y and z coordinates are updated every second so i need the file to update accordingly. S

    Question for you friend:
    You want to write in that file ALL
    and not just last received coordinates?
    If answer is yes that means, that you APPEND new coordinate info to that file, and not write over it. So maybe you need constructor like this:


    Java Code:
    BufferedWriter writer = new BufferedWriter(
    				new FileWriter("myfile.txt",true)) ;
    "append - boolean if true,
    then data will be written to the end of the file rather than the beginning"



    Opening and closing file every second is out of question.
    You either write to some char[] array or String and when job is done
    you write to file only once.

    What kind of printing problems you have?
    Does text becomes written only when you stop client socket code maybe?

    You are using buffers. To remind you:
    Streams and buffers flush on close()
    and there's flushing for buffer only when the buffer is full.
    There are auto flushing cases is some classes and their methods by default like PrintWriters printing the end of the line.
    Default buffer size for a BufferedInputStream is 8KB.

    regards and good luck!

  3. #3
    colossusdub is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Printing Problem

    The printing problems i am having is that it isn't printing anything to the file, no coordinates are being printed at all! the coordinates are only being printed to the shell!!

  4. #4
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    OK you need some time to get it all.

    First you have to find out about protocol - message format that carries info about cooridnates.

    Client and server communicate and client has to know how server send packages.

    Did you manage to print in console coordinates that server send you?

    Should you use that
    Java Code:
    fromServer = in.readLine()
    or maybe something like :

    Java Code:
    int x = 0;
    		
    		while ( (x=bufferedReader.read())!=-1  ) {
    		
    			char c = (char) x;
    			System.out.println(c);
    		}
    What is then end of one cycle?
    I assume if you use :
    in.readLine()
    that "carriage return" or '\r\n' is what is used to separate every cycle.

    If not then readLine() is maybe not appropriate and you have to use
    some other way to read and parse that message


    The printing problems i am having is that it isn't printing anything to the file, no coordinates are being printed at all! the coordinates are only being printed to the shell!!
    Once you print it to console it's all over and your 'fromServer' becomes null
    so line
    outs.write("Server: " + fromServer);
    will not work. So you better remove writing to console or you save all that in some char[] or StringBuffer so you can use that variable to write to file.

    Take care about clients reading.
    You don't want to let client to close himself after reading first message from server!

    Take some time to understand streams,
    Write few basic client/server examples with reading and writing and using
    read() and readline(), and don't close them on first reading in your code.
    After that, move further and be you know exactly messages in your protocol (in what format server sends those coordinate info)

    Streams are not very friendly but don't let that discourage you
    take one step at the time :)

Similar Threads

  1. Real time task scheduling
    By coder_ in forum New To Java
    Replies: 0
    Last Post: 06-25-2009, 03:26 PM
  2. Changing colors of an image in real time
    By chale in forum CLDC and MIDP
    Replies: 0
    Last Post: 05-06-2009, 03:04 PM
  3. real-time validation
    By atomz4peace in forum Eclipse
    Replies: 0
    Last Post: 01-06-2009, 07:31 PM
  4. how get real-time output
    By tOpach in forum New To Java
    Replies: 5
    Last Post: 12-17-2008, 08:41 PM
  5. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •