>> I have to receive and storage this data somewhere continuosly in order to process it, in the meantime I need to keep receiving the server data because I cannot lose any part of the server messages.
The socket will buffer data for you until you read it, but if the buffer becomes full it'll start dropping messages. You need to make sure that you keep reading frequently enough so that the socket doesn't have to discard anything. In other words, you need to keep reading in a loop (as you're doing) and not allow processing the data to block reading the data from the socket. I think you realize this already, based on this statement:
>> if there´s a way to get all the data continuosly to storage it without losing any part of the data flow
To address this part:
>> if there´s a way to not depend of a byte buffer
no, sockets send and receive bytes. If you want to deal directly with sockets then byte[]s are what you have to deal with. That's not the problem with your code, though, it's the processing the data on the same thread as the read that looks dangerous. Depending on how long that takes data could be backing up waiting to be read and that's what you want to avoid. It may be the case that the server doesn't send data frequently and/or parsing it doesn't take very long in which case you'll be fine as is.
If that's not the case then one way or another I think you're going to have to do the data processing on another thread. Exactly how you do that depends on the specifics of your application. What you probably
don't want to do is start a new thread for every message that's received; instead you most likely want one thread that handles every message. This will make it so that each message is handled one at a time and they're handled in the order that they're received. If those things don't matter then you could either start a thread for each message (still may not be optimal) or use the Thread Pool to handle the threads for you (probably better than manual threading).
With either approach, the
while
{
read;
process;
}
loop will change to
while
{
read;
send data to another thread for processing;
}
That way the thread doing the reading won't spend any time on processing and can continue reading data from the socket. There are still some other things to consider:
* exception handling around the code that reads from the socket - currently there is none
* how do the threads doing the reading and parsing exit cleanly? The way that read thread is written there isn't a way.
* how are you going to get data from the read thread to the processing thread? You'll want to read up on thread safety, wait handles and other general threading topics (like
this[
^]). Or post another question asking for help on that.
* does each read you do give you one full message to parse, or might you need to combine the data received from multiple reads into one full message? Or will one read give you multiple messages to parse all at once?
* why is a class that reads data called
Write
?