De: https://www.iteye.com/blog/mars914-1238353
Em java, a comunicação entre sistemas TCP / IP + NIO pode ser realizada com base nas classes relacionadas de Canal e Seletor em java.nio.channels.
Utilizado para comunicação entre sistemas, SocketChannel e ServerSocketChannel são usados. SocketChannel é usado para estabelecer conexões, monitorar eventos e operações de leitura e gravação.ServidorSocketChannel é usado para monitorar portas e monitorar eventos de conexão.Você pode usar o Seletor para obter se há eventos a serem processados.
Código java do servidor:
- pacote com.java.distributed.message.tcpip;
- importar java.io.IOException;
- importar java.net.InetSocketAddress;
- importar java.net.ServerSocket;
- importar java.nio.ByteBuffer;
- importar java.nio.channels.SelectionKey;
- importar java.nio.channels.Selector;
- importar java.nio.channels.ServerSocketChannel;
- importar java.nio.channels.SocketChannel;
- importar java.nio.charset.Charset;
- classe pública NIOServer {
- / **
- * @param args
- * @throws IOException
- * /
- public static void main (String [] args) lança IOException {
- porta int = 7889;
- // Abra o seletor
- Seletor seletor = Selector.open ();
- // Abra o canal do soquete do servidor
- ServerSocketChannel ssc = ServerSocketChannel.open ();
- // Recupera o soquete do servidor associado a este canal
- ServerSocket serverSocket = ssc.socket ();
- // Vincula ServerSocket a um endereço específico (endereço IP e número da porta)
- serverSocket.bind ( novo InetSocketAddress (porta));
- System.out.println ( "servidor escuta na porta:" + porta);
- // Ajusta o modo de bloqueio do canal
- ssc.configureBlocking ( false);
- // Registre este canal com o seletor fornecido e retorne uma tecla de seleção. SelectionKey.OP_ACCEPT - Bit de conjunto de operações para operação de aceitação de soquete
- ssc.register (seletor, SelectionKey.OP_ACCEPT);
- while ( true) {
- // timeout: positivo, em seguida, bloqueie no máximo o tempo limite ms enquanto aguarda que um canal esteja pronto; se zero, bloqueie indefinidamente; deve ser não negativo
- int nKeys = selector.select ( 1000);
- if (nKeys> 0) {
- for (chave SelectionKey: selector.selectedKeys ()) {
- / * Teste se o canal desta chave está pronto para aceitar novas conexões de soquete -
- * Se o canal desta chave não suportar operação de aceitação de soquete, esse método sempre retornará false
- * * /
- if (key.isAcceptable ()) {
- ServerSocketChannel server = (ServerSocketChannel) key.channel ();
- SocketChannel sc = server.accept ();
- if (sc == null) {
- continuar;
- }
- sc.configureBlocking ( false);
- sc.register (seletor, SelectionKey.OP_READ);
- } else if (key.isReadable ()) {
- // Aloca um novo buffer de bytes
- Buffer ByteBuffer = ByteBuffer.allocate ( 1024);
- SocketChannel sc = (SocketChannel) key.channel ();
- int readBytes = 0;
- Mensagem da string = null;
- experimentar{
- int ret;
- experimentar{
- while ((ret = sc.read (buffer))> 0) {
- readBytes + = ret;
- }
- } captura (exceção e) {
- readBytes = 0;
- //ignorar
- } finalmente {
- // Inverta esse buffer. Primeiro defina um limite na posição atual e depois defina a posição como zero
- buffer.flip ();
- }
- if (readBytes> 0) {
- message = Charset.forName ( "UTF-8"). decode (buffer) .toString ();
- buffer = nulo;
- }
- } finalmente {
- if (buffer! = nulo)
- buffer.clear ();
- }
- if (readBytes> 0) {
- System.out.println ( "mensagem do cliente:" + mensagem);
- if ( "quit" .equalsIgnoreCase (message.trim ())) {
- sc.close ();
- selector.close ();
- System.out.println ( "O servidor foi encerrado!");
- System.exit ( 0);
- }
- String outMessage = "resposta do servidor:" + mensagem;
- sc.write (Charset.forName ( "UTF-8"). encode (outMessage));
- }
- }
- }
- selector.selectedKeys (). clear ();
- }
- }
- }
- }
Código java do cliente:
- pacote com.java.distributed.message.tcpip;
- importar java.io.BufferedReader;
- importar java.io.IOException;
- importar java.io.InputStreamReader;
- importar java.net.InetSocketAddress;
- importar java.net.SocketAddress;
- importar java.nio.ByteBuffer;
- importar java.nio.channels.SelectionKey;
- importar java.nio.channels.Selector;
- importar java.nio.channels.SocketChannel;
- importar java.nio.charset.Charset;
- classe pública NIOClient {
- / **
- * @param args
- * @throws IOException
- * /
- public static void main (String [] args) lança IOException {
- porta int = 7889;
- Canal SocketChannel = SocketChannel.open ();
- channel.configureBlocking ( false);
- SocketAddress target = new InetSocketAddress ( "127.0.0.1", porta);
- channel.connect (target);
- Seletor seletor = Selector.open ();
- // Bit de conjunto de operações para operação de conexão de soquete
- channel.register (seletor, SelectionKey.OP_CONNECT);
- BufferedReader systemIn = novo BufferedReader ( novo InputStreamReader (System.in));
- while ( true) {
- if (channel.isConnected ()) {
- Comando string = systemIn.readLine ();
- channel.write (Charset.forName ( "UTF-8"). encode (comando));
- if (command == null || "quit" .equalsIgnoreCase (command.trim ())) {
- systemIn.close ();
- channel.close ();
- selector.close ();
- System.out.println ( "Cliente encerrado!");
- System.exit ( 0);
- }
- }
- int nKeys = selector.select ( 1000);
- if (nKeys> 0) {
- for (chave SelectionKey: selector.selectedKeys ()) {
- if (key.isConnectable ()) {
- SocketChannel sc = (SocketChannel) key.channel ();
- sc.configureBlocking ( false);
- sc.register (seletor, SelectionKey.OP_READ);
- sc.finishConnect ();
- } else if (key.isReadable ()) {
- Buffer ByteBuffer = ByteBuffer.allocate ( 1024);
- SocketChannel sc = (SocketChannel) key.channel ();
- int readBytes = 0;
- experimentar{
- int ret = 0;
- experimentar{
- while ((ret = sc.read (buffer))> 0) {
- ler bytes = K +;
- }
- } finalmente {
- buffer.flip ();
- }
- if (readBytes> 0) {
- System.out.println (Charset.forName ( "UTF-8")
- .decode (buffer) .toString ());
- buffer = nulo;
- }
- } finalmente {
- if (buffer! = null) {
- buffer.clear ();
- }
- }
- }
- }
- selector.selectedKeys (). clear ();
- }
- }
- }
- }