Petite démonstration:
Code côté serveur
emballer DEMONIO; importation com.oracle.nio.BufferSecrets; importation java.io.IOException; importation java.net.InetSocketAddress; importation java.nio.Buffer; importation java.nio.ByteBuffer; importation java.nio.channels.SelectionKey; importation java.nio.channels.Selector; importation java.nio.channels.ServerSocketChannel; importation java.nio.channels.SocketChannel; importation java.util.Iterator; importation java.util.Set; publique classe NioServerWithSelector { publique statique vide principale (String [] args)jette IOException { // obtenir le canal de coupure du serveur un ServerSocketChannel ServerSocketChannel = ServerSocketChannel.open (); // port de liaison serverSocketChannel.socket () la liaison (. nouvelle nouvelle du InetSocketAddress (6666 )); // ensemble à asynchrone serverSocketChannel.configureBlocking ( false ); // Création de sélection, un écran de sélection Sélecteur Sélecteur = Selector.open (); // registre (paramètres OPS pour la connexion) serverSocketChannel.register (Sélecteur, SelectionKey.OP_ACCEPT), le tout en ( true ) { // dans les 2 secondes personne reliant le tout (le selector.select (2000) == 0 ) { System.out.println ( "attente de connexion" ); Continuer ; } // préparez - clés Set <SelectionKey> Touches = selector.selectedKeys (); // Iterate Touches Iterator <SelectionKey> Iterator = keys.iterator (), le tout en (iterator.hasNext ()) { // obtenir la clé SelectionKey Key = Iterator.next (); // si l'événement de connexion IF (key.isAcceptable ()) { //Le premier canal de connexion pour obtenir le SocketChannel SocketChannel = ServerSocketChannel.accept (); IF (SocketChannel =! Null ) { System.out.println (Thread.currentThread () getName (). + ": Réception d' une connexion" + SocketChannel. getRemoteAddress () + "est envoyé du port:" + socketChannel.getRemoteAddress ()); // ensemble conduit asynchrone socketChannel.configureBlocking ( false ); // registre socketChannel.register (sélecteur, SelectionKey.OP_READ, ByteBuffer.allocate (1024 ) ); } } // Si l'événement est lu IF (key.isReadable ()) { // obtenir le canal, obtenir directement par la clé! ! = SocketChannel la SocketChannel (le SocketChannel) key.channel (); // obtenir des tampons du tampon ByteBuffer = (le ByteBuffer) key.attachment (); int longueur = socketChannel.read (tampon); IF (! Longueur = -1 ) { le système. out.println (. Thread.currentThread () getName () + ": les données reçues sont:" + new new String (buffer.array (), 0 , longueur)); } // caches claires buffer.clear (); } Iterator.remove (); } } } }
Code client:
emballer DEMONIO; importation java.io.IOException; importation java.net.InetAddress; importation java.net.InetSocketAddress; importation java.net.Socket; publique classe NioClient { publics statiques vides principaux (String [] args) lance IOException { InetSocketAddress SocketAddress = nouvelle InetSocketAddress (InetAddress.getLocalHost (), 6666 ); Socket = nouveau Socket (); Socket.connect (SocketAddress); socket.getOutputStream (). écriture ( "bonjour".getBytes ()); } }