Reputation: 111
I'm a newbie on Netty, and I'm wondering how to read/handle incoming TCP data when the TCP client sometimes sends variable length binary records (hex) and sometimes sends variable length ASCII records, none of which are delimited by carriage return or line feed.
The TCP client is sending a stream of bytes as follows:
A binary stream of bytes that begins with '$' but has a varying number of bytes and not terminated by any character, and
An ASCII stream of bytes that begins with '(' and ends with ')' but has a varying number of bytes and not terminated by any character
Both sets of records arrive on the same port.
How would I need to code my Netty-based TCP server to handle/read both?
Thanks in advance.
Kunal
Upvotes: 3
Views: 2319
Reputation: 23567
Checkout the port unification example. It does exactly what you want.
Upvotes: 5
Reputation: 2129
First, don't mix everything together '$' is a Char, it can be represented by one single Byte in most encoding but this is hardly the case for a common char (in UTF-8, char a variable length encoded and can use 1, 2 or 3 Bytes).
It depends the length of your stream.
1) The easiest solution, if it can fit inside the memory of your sender, is to use a frame with a two fields header:
Than, you could use a FrameDecoder to receive and decode the whole frame.
2) If your stream is huge and cannot fit inside the memory, you have two solutions:
In every case Netty prodives some StringEncoder and StringDecoder for streaming Strings. And also some FrameDecoder and FrameEncoder with all kinds of usefull headers.
Upvotes: 0