How to realize the chat room function of Java based on NIO
Java based on NIO how to achieve chat room function, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Sever end
Package com.qst.one;import java.io.IOException;import java.net.InetSocketAddress;import java.net.SocketAddress;import java.nio.channels.Channel;import java.nio.channels.SelectableChannel;import java.nio.channels.SelectionKey;import java.nio.channels.Selector;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;import java.util.Iterator;import java.util.Set;import java.nio.ByteBuffer;public class Server {private static SocketChannel accept Public static void main (String [] args) {System.out.println ("- server startup -"); try {/ / get channel ServerSocketChannel channel = ServerSocketChannel.open (); / / configure non-blocking mode channel.configureBlocking (false); / / bind connected port channel.bind (new InetSocketAddress (9999)); / / get selector Selector selector = Selector.open () / / register the channel to the selector and start listening to the event channel.register (selector, SelectionKey.OP_ACCEPT); / / use the selector to poll while (selector.select () > 0) {/ / get the event Iterator iterator = selector.selectedKeys () .iterator () in all the registered channels on the selector. While (iterator.hasNext ()) {/ / get the event SelectionKey next = iterator.next (); / / determine the event type if (next.isAcceptable ()) {/ / get the channel accept = channel.accept (); / / get the current connection assignment address SocketAddress address = accept.getLocalAddress (); System.out.println (address + "online") / / switch mode accept.configureBlocking (false); / / register the channel to the selector accept.register (selector, SelectionKey.OP_READ);} / / if read mode else if (next.isReadable ()) {SocketChannel accept = (SocketChannel) next.channel (); / / read event ByteBuffer buffer = ByteBuffer.allocate (1024); int len While ((len = accept.read (buffer)) > 0) {/ / enable read mode buffer.flip (); / / System.out.println ((char) len); System.out.println (new String (buffer.array (), 0, len)); / / homing buffer.clear ();}} iterator.remove () } catch (Exception e) {try {SocketAddress address = accept.getRemoteAddress (); System.out.println (address+ "offline");} catch (IOException E1) {/ / TODO Auto-generated catch block e1.printStackTrace ();}
Client end
Package com.qst.one;import java.io.IOException;import java.net.InetSocketAddress;import java.net.SocketAddress;import java.nio.channels.SocketChannel;import java.util.Scanner;import java.nio.ByteBuffer;public class Client {public static void main (String [] args) throws IOException {SocketChannel channel = SocketChannel.open (new InetSocketAddress ("localhost", 9999)); channel.configureBlocking (false); ByteBuffer buffer = ByteBuffer.allocate (1024); Scanner sc = new Scanner (System.in); SocketAddress address = channel.getLocalAddress () System.out.println (address+ "ready~~~"); while (true) {System.out.print ("tim:"); String name = sc.nextLine (); buffer.put (("tim:" + name). GetBytes ()); buffer.flip (); channel.write (buffer); buffer.clear ();}
After reading the above, have you mastered the method of how Java implements the function of chat room based on NIO? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!