문제

I'm looking for an efficient solution to receive and process asynchronous messages (of varying size) from a socket connection to a server. It is a good amount of bandwidth coming over (maybe 250 kB/s steady state, and can have short bursts up to 1 MB/s). Currently I'm using a DataInputStream, but during busy moments I get disconnected (the server disconnects a client if the queue backlog grows too large).

Can anyone confirm if a BufferedInputStream may work better? I've read that it will retrieve chunks of data at a time, rather than byte by byte, so there are fewer OS calls.

Thanks!

도움이 되었습니까?

해결책

It does retrieve chunks at a time, and it could have fewer OS calls, but this won't help if you aren't calling read fast enough to service the backlog. It won't read the next chunk until you fully read through the chunk it prefetched.

Probably the best thing to do is to constantly stay on top of the InputStream (read it whenever it has something to read) and do any other processing later or in a another (lower priority) thread.

다른 팁

I'm no expert but I do know that a BufferedInputStream will in fact read a chunk at a time because it will use a buffer that is X bytes big. It is essentially an abstraction for reading data over a socket, rather than having it use system calls like you said. It may help with the disconnecting issue. Sorry if I reiterated on some of your points. Basically, I would suggest using the Buffered stream. It is more controlled, a few Google searches would give you a much better insight.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top