views:

216

answers:

1

Hello all,

I have made a socket listener in java that listens on 2 ports for data and does operation on the listened data. Now the scenario is such that when both the listener and the device that transmits data are up and running, the listener receives data, one at a time ( each data starts with a "#S" and ends with a ".") and when the listener is not up or is not listening, the device stores the data in its local memory and as soon as the listener is up it sends all the data in the appended form ie like

"#S ...DATA...[.]#S...DATA...[.]..."

Now i have implemented this in a way that, whatever data the listener gets on either port, it converts into the hex form, and then carries out operations on the hex format of the input data.The hex form of"#S" is "2353" and the hex form of "." is "2e". The code for handling the hex-converted form of the input data is as follows. 'hexconverted1' is a string that contains the hex-converted form of the whole input data, that comes on any port.

  String store[];
store=hexconverted1.split("2353"); 
for(int m=0;m<store.length;m++)
    store[m]="2353"+store[m];

 PrintWriter out2 = new PrintWriter(new BufferedWriter(new FileWriter("C:/Listener/array.bin", true)));
    for(int iter=0;iter<store.length; iter++)
        out2.println(store[iter]);
    out2.close();

What i am trying to accomplish by the above code is that, whenever a bunch of data arrives, im trying to scan through the data and sore every single data from the bunch and store in in a string array so that the operations i wish to carry out on the hex converted form of the data can be done in an easier manner. So when i write the contents of the array to a BIN file,the output varies for the same input. When i send a bunched data of 280 data packets, appended one after the other, at times, the array contains 180, at other times 270. But for smaller bunch sizes i get the desired results and the size of the 'store' array is also as expected.

Im pretty clueless about whats going on and any pointers would be of great help.

To make matters more lucid, The data i get on the ports are mostly unreadable and often the only readable parts are the starting bits"#S" and the end bit".". So im using a combination of BufferedInputStream and InputStream to read the incoming data and convert it into the hex format and i quite sure that the conversion to hex is coimg about alright.

A: 

im using a combination of BufferedInputStream and InputStream to read the incoming data

Clutching at straws here. If you read from a Stream using both InputStream and BufferedInputStream methods, you'll get into difficulty:

InputStream is = ...
BufferedInputStream bis = new BufferedInputStream(is);

// This is OK
int b = bis.read();
...
// Reading the InputStream directly at this point is liable to
// give unpredictable results.  It is likely that some bytes still
// remain in "bis"'s buffer, and a read on "is" will not return them.
int b2 = is.read();
Stephen C