0

Suppose I get from the network a sequence of bytes in the following order: X X X X = X X X X = X X X X = ... and so on you can notice immediately the pattern (a message is a sequence of 5 bytes: "X X X X =
X can be any number except '=' of course)

Now, the transmission occur periodically. In some point my application is starting and getting the bytes sequence

What is the best algorithm for taking those bytes and framing them to 5 bytes messages?
There are two problems:
1. How do you find the first message? I guess I need to drop some bytes till I get '=' right?
2. How the application suppose to handle a pause of transimmision and a new start.

4

1 に答える 1

0

最も簡単な方法は、一度に 5 バイトを読み取るためにブロック読み取り (最も単純なため最も一般的です) を使用することです。

通常、最初からジャンク データがあるとは想定せず、すべてを読み取るだけです。ヘッダーがあれば、それも読みます。

DataInputStream dis = new DataInputStream(socket stream);
byte[] bytes = new byte[5];

int sep;
do {
  dis.readFully(bytes);
  process(bytes);
} while((sep = dis.read()) == '=');
if (sep > 0)
   System.err.println("Unexpected character "+ (char) sep));
于 2012-05-07T07:04:37.580 に答える