In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.