Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example usage of java NIO SocketClinet and ServerSocket

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report