Example usage of java NIO SocketClinet and ServerSocket
This article introduces the knowledge of "example usage of java NIO SocketClinet and ServerSocket". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
/ / SocketClient.java//----package niotest;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;import java.nio.channels.Selector Import java.nio.channels.SocketChannel;import java.util.Iterator;import java.util.Set;public class SocketClient {public static void main (String [] args) throws IOException {Selector sc = Selector.open (); SocketChannel skc = SocketChannel.open (); skc.configureBlocking (false); skc.connect (new InetSocketAddress ("127.0.0.1", 44444)); skc.register (sc, SelectionKey.OP_CONNECT); while (true) {sc.select (); Set selectkey = sc.selectedKeys (); Iterator it = selectkey.iterator () While (it.hasNext ()) {SelectionKey key = (SelectionKey) it.next (); if (key.isConnectable ()) {SocketChannel msk = (SocketChannel) key.channel (); if (! msk.isConnected ()) {while (! msk.finishConnect ()) {}} msk.register (sc, SelectionKey.OP_WRITE);} else if (key.isReadable ()) {} else if (key.isWritable ()) {SocketChannel msk = (SocketChannel) key.channel () ByteBuffer bb = ByteBuffer.wrap (new String ("hellow world!"). GetBytes (); msk.write (bb); while (bb.hasRemaining ()) {msk.write (bb);} key.cancel ();} else if (key.isAcceptable ()) {} else {throw new RuntimeException ("unknow selection type!");} it.remove () } / / SocketServer.java//-package niotest;import java.io.IOException Import java.net.InetSocketAddress;import java.net.SocketAddress;import java.nio.ByteBuffer;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;public class SocketServer {public static void main (String [] args) throws Exception {ServerSocketChannel sc = ServerSocketChannel.open (); sc.configureBlocking (false); sc.bind (new InetSocketAddress (44444)); Selector tor = Selector.open () Sc.register (tor, SelectionKey.OP_ACCEPT); while (true) {tor.select (); Set selectkey = tor.selectedKeys (); Iterator it = selectkey.iterator (); while (it.hasNext ()) {SelectionKey key = (SelectionKey) it.next (); if (key.isAcceptable ()) {ServerSocketChannel msk = (ServerSocketChannel) key.channel (); SocketChannel msc = msk.accept (); msc.configureBlocking (false); msc.register (tor, SelectionKey.OP_READ) } else if (key.isConnectable ()) {} else if (key.isWritable ()) {} else if (key.isReadable ()) {SocketChannel msc = (SocketChannel) key.channel (); ByteBuffer bb = ByteBuffer.allocate (17); while (bb.hasRemaining ()) {msc.read (bb);} System.out.println ("server:" + new String (bb.array ();} else {throw new RuntimeException ("unknow selection!");} it.remove () This is the end of the introduction of "example usage of java NIO SocketClinet and ServerSocket". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!