开发者

Java socket listener load problem

I have made a socket listener in Java that listens on two ports for data and does operations 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 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, I'm 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.

I'm 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 I'm using a combination of BufferedInputStream and InputStream to read the incoming data and convert it into the hex format and I'm quite sure that the conversion to hex is coming about alright.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜