這篇文章主要講解了“Java NIO代碼怎么寫(xiě)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Java NIO代碼怎么寫(xiě)”吧!
定陶網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,定陶網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為定陶上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的定陶做網(wǎng)站的公司定做!
Java代碼:
import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; 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 NIOServer { /*標(biāo)識(shí)數(shù)字*/ private int flag = 0; /*緩沖區(qū)大小*/ private int BLOCK = 4096; /*接受數(shù)據(jù)緩沖區(qū)*/ private ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK); /*發(fā)送數(shù)據(jù)緩沖區(qū)*/ private ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK); private Selector selector; public NIOServer(int port) throws IOException { // 打開(kāi)服務(wù)器套接字通道 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); // 服務(wù)器配置為非阻塞 serverSocketChannel.configureBlocking(false); // 檢索與此通道關(guān)聯(lián)的服務(wù)器套接字 ServerSocket serverSocket = serverSocketChannel.socket(); // 進(jìn)行服務(wù)的綁定 serverSocket.bind(new InetSocketAddress(port)); // 通過(guò)open()方法找到Selector selector = Selector.open(); // 注冊(cè)到selector,等待連接 serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); System.out.println("Server Start----8888:"); } // 監(jiān)聽(tīng) private void listen() throws IOException { while (true) { // 選擇一組鍵,并且相應(yīng)的通道已經(jīng)打開(kāi) selector.select(); // 返回此選擇器的已選擇鍵集。 Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey selectionKey = iterator.next(); iterator.remove(); handleKey(selectionKey); } } } // 處理請(qǐng)求 private void handleKey(SelectionKey selectionKey) throws IOException { // 接受請(qǐng)求 ServerSocketChannel server = null; SocketChannel client = null; String receiveText; String sendText; int count=0; // 測(cè)試此鍵的通道是否已準(zhǔn)備好接受新的套接字連接。 if (selectionKey.isAcceptable()) { // 返回為之創(chuàng)建此鍵的通道。 server = (ServerSocketChannel) selectionKey.channel(); // 接受到此通道套接字的連接。 // 此方法返回的套接字通道(如果有)將處于阻塞模式。 client = server.accept(); // 配置為非阻塞 client.configureBlocking(false); // 注冊(cè)到selector,等待連接 client.register(selector, SelectionKey.OP_READ); } else if (selectionKey.isReadable()) { // 返回為之創(chuàng)建此鍵的通道。 client = (SocketChannel) selectionKey.channel(); //將緩沖區(qū)清空以備下次讀取 receivebuffer.clear(); //讀取服務(wù)器發(fā)送來(lái)的數(shù)據(jù)到緩沖區(qū)中 count = client.read(receivebuffer); if (count > 0) { receiveText = new String( receivebuffer.array(),0,count); System.out.println("服務(wù)器端接受客戶(hù)端數(shù)據(jù)--:"+receiveText); client.register(selector, SelectionKey.OP_WRITE); } } else if (selectionKey.isWritable()) { //將緩沖區(qū)清空以備下次寫(xiě)入 sendbuffer.clear(); // 返回為之創(chuàng)建此鍵的通道。 client = (SocketChannel) selectionKey.channel(); sendText="message from server--" + flag++; //向緩沖區(qū)中輸入數(shù)據(jù) sendbuffer.put(sendText.getBytes()); //將緩沖區(qū)各標(biāo)志復(fù)位,因?yàn)橄蚶锩鎝ut了數(shù)據(jù)標(biāo)志被改變要想從中讀取數(shù)據(jù)發(fā)向服務(wù)器,就要復(fù)位 sendbuffer.flip(); //輸出到通道 client.write(sendbuffer); System.out.println("服務(wù)器端向客戶(hù)端發(fā)送數(shù)據(jù)--:"+sendText); client.register(selector, SelectionKey.OP_READ); } } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int port = 8888; NIOServer server = new NIOServer(port); server.listen(); } }
Java代碼:
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 NIOClient { /*標(biāo)識(shí)數(shù)字*/ private static int flag = 0; /*緩沖區(qū)大小*/ private static int BLOCK = 4096; /*接受數(shù)據(jù)緩沖區(qū)*/ private static ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK); /*發(fā)送數(shù)據(jù)緩沖區(qū)*/ private static ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK); /*服務(wù)器端地址*/ private final static InetSocketAddress SERVER_ADDRESS = new InetSocketAddress( "localhost", 1111); public static void main(String[] args) throws IOException { // TODO Auto-generated method stub // 打開(kāi)socket通道 SocketChannel socketChannel = SocketChannel.open(); // 設(shè)置為非阻塞方式 socketChannel.configureBlocking(false); // 打開(kāi)選擇器 Selector selector = Selector.open(); // 注冊(cè)連接服務(wù)端socket動(dòng)作 socketChannel.register(selector, SelectionKey.OP_CONNECT); // 連接 socketChannel.connect(SERVER_ADDRESS); // 分配緩沖區(qū)大小內(nèi)存 Set<SelectionKey> selectionKeys; Iterator<SelectionKey> iterator; SelectionKey selectionKey; SocketChannel client; String receiveText; String sendText; int count=0; while (true) { //選擇一組鍵,其相應(yīng)的通道已為 I/O 操作準(zhǔn)備就緒。 //此方法執(zhí)行處于阻塞模式的選擇操作。 selector.select(); //返回此選擇器的已選擇鍵集。 selectionKeys = selector.selectedKeys(); //System.out.println(selectionKeys.size()); iterator = selectionKeys.iterator(); while (iterator.hasNext()) { selectionKey = iterator.next(); if (selectionKey.isConnectable()) { System.out.println("client connect"); client = (SocketChannel) selectionKey.channel(); // 判斷此通道上是否正在進(jìn)行連接操作。 // 完成套接字通道的連接過(guò)程。 if (client.isConnectionPending()) { client.finishConnect(); System.out.println("完成連接!"); sendbuffer.clear(); sendbuffer.put("Hello,Server".getBytes()); sendbuffer.flip(); client.write(sendbuffer); } client.register(selector, SelectionKey.OP_READ); } else if (selectionKey.isReadable()) { client = (SocketChannel) selectionKey.channel(); //將緩沖區(qū)清空以備下次讀取 receivebuffer.clear(); //讀取服務(wù)器發(fā)送來(lái)的數(shù)據(jù)到緩沖區(qū)中 count=client.read(receivebuffer); if(count>0){ receiveText = new String( receivebuffer.array(),0,count); System.out.println("客戶(hù)端接受服務(wù)器端數(shù)據(jù)--:"+receiveText); client.register(selector, SelectionKey.OP_WRITE); } } else if (selectionKey.isWritable()) { sendbuffer.clear(); client = (SocketChannel) selectionKey.channel(); sendText = "message from client--" + (flag++); sendbuffer.put(sendText.getBytes()); //將緩沖區(qū)各標(biāo)志復(fù)位,因?yàn)橄蚶锩鎝ut了數(shù)據(jù)標(biāo)志被改變要想從中讀取數(shù)據(jù)發(fā)向服務(wù)器,就要復(fù)位 sendbuffer.flip(); client.write(sendbuffer); System.out.println("客戶(hù)端向服務(wù)器端發(fā)送數(shù)據(jù)--:"+sendText); client.register(selector, SelectionKey.OP_READ); } } selectionKeys.clear(); } } }
感謝各位的閱讀,以上就是“Java NIO代碼怎么寫(xiě)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Java NIO代碼怎么寫(xiě)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)頁(yè)題目:JavaNIO代碼怎么寫(xiě)
標(biāo)題網(wǎng)址:http://vcdvsql.cn/article44/gdjoee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、域名注冊(cè)、App設(shè)計(jì)、ChatGPT、品牌網(wǎng)站制作、用戶(hù)體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
營(yíng)銷(xiāo)型網(wǎng)站建設(shè)知識(shí)