Results 1 to 4 of 4
- 03-02-2010, 02:36 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
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(); } }
- 03-03-2010, 01:03 AM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
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:
"append - boolean if true,Java Code:BufferedWriter writer = new BufferedWriter( new FileWriter("myfile.txt",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!
- 03-03-2010, 01:15 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
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!!
- 03-03-2010, 07:32 PM #4
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
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
or maybe something like :Java Code:fromServer = in.readLine()
What is then end of one cycle?Java Code:int x = 0; while ( (x=bufferedReader.read())!=-1 ) { char c = (char) x; System.out.println(c); }
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
Once you print it to console it's all over and your 'fromServer' becomes nullThe 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!!
so line
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.outs.write("Server: " + fromServer);
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
-
Real time task scheduling
By coder_ in forum New To JavaReplies: 0Last Post: 06-25-2009, 03:26 PM -
Changing colors of an image in real time
By chale in forum CLDC and MIDPReplies: 0Last Post: 05-06-2009, 03:04 PM -
real-time validation
By atomz4peace in forum EclipseReplies: 0Last Post: 01-06-2009, 07:31 PM -
how get real-time output
By tOpach in forum New To JavaReplies: 5Last Post: 12-17-2008, 08:41 PM -
Saving data in an XML file
By Thez in forum New To JavaReplies: 1Last Post: 12-08-2007, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks