P
P
parasit322016-08-14 20:35:31
Java
parasit32, 2016-08-14 20:35:31

How to work with NIO?

I'm trying to immerse myself in NIO, but reading articles in my head is a mess.
How to identify a client that has already sent data to the server? Is there already a ready implementation of the session? How can I make it so that the server can send data to the client at any time? Is it worth creating several handler threads or is NIO designed for one? Is there an example of some fully working service in which you can look at how requests for different actions by clients, serialization and everything else are implemented? Is there any clear manual / tutorial / course in which both the basics and things that are not obvious at first sight are explained? In those articles that I saw, there were only simple snippets and a brief explanation of which object is responsible for what, but I still did not understand how it should work all together in a real system.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2016-08-15
@jamakasi666

It's a very broad question and you won't find an answer of that size.
I can only suggest one. Try to write your chat gradually complicating it.
For example. There is a class user in which there are fields ip_addres\username.
There is a certain abstract class packet in it there are fields pack_num, byte_data. There is also a successor pack_message which has a new message field. And pack_auth with username field.
pack_num = unique package number to understand what to serialize or deserialize into.
byte_data = byte representation of the contents of the package.
Then write (or take a snippet) of the server\client. The server is waiting for data, the client sends a pack_auth packet with its username, the server creates a stream and a user instance in which it enters the received data. Further, the client and server can change their own pack_message packages.
When you've played enough, complicate the task, enter a new type of broadcast packet, rooms, message delivery notification, user status, etc. making the task ever more difficult. Ultimately, you will understand how it all works, and in particular, you will be able to take a new topic with a challenge for yourself and implement it.
Я лично только после такого смог более или менее разобраться с nio, а чат просто потому что это не так сложно, интересно и охватывает очень многие темы работы с nio. Еще кстати заодно и заюзал eventbus из либы guava для событийно ориентированного кода а позже написал свою версию библиотеки для событий и наживую вкинул в чат.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question