中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

專門做瓷磚的網(wǎng)站百度推廣客服工作怎么樣

專門做瓷磚的網(wǎng)站,百度推廣客服工作怎么樣,免費網(wǎng)站建設(shè)知識,免費網(wǎng)站建設(shè)設(shè)計制作公司文章目錄 Java網(wǎng)絡(luò)編程原理與實踐--從Socket到BIO再到NIOSocket基本架構(gòu)Socket 基本使用簡單一次發(fā)送接收客戶端服務(wù)端 字節(jié)流方式簡單發(fā)送接收客戶端服務(wù)端 雙向通信客戶端服務(wù)端 多次接收消息客戶端服務(wù)端 Socket寫法的問題BIO簡單流程BIO寫法客戶端服務(wù)端 BIO的問題 NIO簡述…

文章目錄

  • Java網(wǎng)絡(luò)編程原理與實踐--從Socket到BIO再到NIO
    • Socket基本架構(gòu)
    • Socket 基本使用
      • 簡單一次發(fā)送接收
        • 客戶端
        • 服務(wù)端
      • 字節(jié)流方式簡單發(fā)送接收
        • 客戶端
        • 服務(wù)端
      • 雙向通信
        • 客戶端
        • 服務(wù)端
      • 多次接收消息
        • 客戶端
        • 服務(wù)端
    • Socket寫法的問題
    • BIO
      • 簡單流程
      • BIO寫法
        • 客戶端
        • 服務(wù)端
      • BIO的問題
    • NIO
      • 簡述
      • Buffer
      • Channel(通道)
      • Selector(選擇器)
        • 基本介紹
        • 使用實例

Java網(wǎng)絡(luò)編程原理與實踐–從Socket到BIO再到NIO

Socket基本架構(gòu)

圖來源:https://zhuanlan.zhihu.com/p/462497498
既然是網(wǎng)絡(luò)的東西肯定得放個網(wǎng)絡(luò)架構(gòu)圖,這張圖不多說,感興趣可以去鏈接詳細看一下
在這里插入圖片描述

Socket 基本使用

轉(zhuǎn)自:https://blog.csdn.net/a78270528/article/details/80318571

簡單一次發(fā)送接收

客戶端
package Scoket.client;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;/**** 字符流方式*/
public class Client {public static void main(String[] args) {try {// 服務(wù)器的主機和端口String serverHost = "127.0.0.1";int serverPort = 6443;// 創(chuàng)建Socket對象,連接到服務(wù)器Socket socket = new Socket(serverHost, serverPort);// 獲取輸入流和輸出流BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));PrintWriter output = new PrintWriter(socket.getOutputStream(), true);// 發(fā)送數(shù)據(jù)給服務(wù)器String messageToSend = "Hello, Server!";output.println(messageToSend);// 接收服務(wù)器的響應數(shù)據(jù)String dataReceived = input.readLine();System.out.println("Received from server: " + dataReceived);// 關(guān)閉連接socket.close();} catch (Exception e) {e.printStackTrace();}}
}
服務(wù)端
package Scoket.client;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;public class Server {public static void main(String[] args) {try {// 本地主機和端口String serverHost = "127.0.0.1";int serverPort = 6443;// 創(chuàng)建ServerSocket對象,綁定地址和端口ServerSocket serverSocket = new ServerSocket(serverPort);System.out.println("Server listening on " + serverHost + ":" + serverPort);// 接受客戶端連接Socket clientSocket = serverSocket.accept();System.out.println("Accepted connection from " + clientSocket.getInetAddress());// 獲取輸入流和輸出流BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);// 接收客戶端發(fā)送的數(shù)據(jù)String dataReceived = input.readLine();System.out.println("Received from client: " + dataReceived);// 發(fā)送響應給客戶端String messageToSend = "Hello, Client!";output.println(messageToSend);// 關(guān)閉連接clientSocket.close();serverSocket.close();} catch (Exception e) {e.printStackTrace();}}
}

如果進行debug會發(fā)現(xiàn),服務(wù)端代碼總共卡主兩次:
1、 Socket clientSocket = serverSocket.accept(); 這里會監(jiān)聽端口,等待客戶端請求建立連接,實際上是進行三次握手
2、 String dataReceived = input.readLine(); 這里是等待客戶端發(fā)送數(shù)據(jù),接收到數(shù)據(jù)會進行下一步
這兩步驟需要注意,因為這是后面BIO和NIO的優(yōu)化點

字節(jié)流方式簡單發(fā)送接收

使用字節(jié)流處理,這可能使得處理字符串數(shù)據(jù)稍顯繁瑣。如果你的通信數(shù)據(jù)是文本,可能使用字符流更為方便。
但是數(shù)據(jù)更可控一些,下面簡單羅列

客戶端
package Scoket.client;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;/*** 字節(jié)流方式*/
public class Client1 {public static void main(String[] args) {try {String host = "127.0.0.1";int port = 6443;Socket socket = new Socket(host, port);OutputStream outputStream = socket.getOutputStream();String message = "message, 你好";socket.getOutputStream().write(message.getBytes(StandardCharsets.UTF_8));outputStream.close();socket.close();} catch (IOException e) {e.printStackTrace();}}
}
服務(wù)端
package Scoket.client;import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;public class Server1 {public static void main(String[] args) {try {int port = 6443;ServerSocket serverSocket = new ServerSocket(port);System.out.println("等待連接");Socket accept = serverSocket.accept();System.out.println("完成連接,等待傳輸數(shù)據(jù)");InputStream inputStream = accept.getInputStream();byte[] bytes = new byte[1024];int len;StringBuilder sb = new StringBuilder();while ((len = inputStream.read(bytes)) != -1){sb.append(new String(bytes, 0, len, "UTF-8"));}System.out.println("get message:" + sb);inputStream.close();accept.close();serverSocket.close();} catch (IOException e) {e.printStackTrace();}}
}

雙向通信

客戶端
package Scoket.client;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;public class Client2 {public static void main(String[] args) {try {String host = "127.0.0.1";int port = 8443;Socket socket = new Socket(host, port);OutputStream outputStream = socket.getOutputStream();outputStream.write("我是客戶,接受一下我的消息".getBytes(StandardCharsets.UTF_8));socket.shutdownOutput();InputStream inputStream = socket.getInputStream();byte[] bytes = new byte[1024];int len;StringBuilder stringBuilder = new StringBuilder();while ((len = inputStream.read(bytes)) != -1){stringBuilder.append(new String(bytes, 0, len, "UTF-8"));}System.out.println("get message:" + stringBuilder);inputStream.close();outputStream.close();socket.close();} catch (IOException e) {e.printStackTrace();}}
}
服務(wù)端
package Scoket.client;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;public class Server2 {public static void main(String[] args) {try {int port = 8443;ServerSocket serverSocket = new ServerSocket(port);Socket socket = serverSocket.accept();InputStream inputStream = socket.getInputStream();byte[] bytes = new byte[1024];int len ;StringBuilder sb = new StringBuilder();while ((len = inputStream.read(bytes)) != -1){sb.append(new String(bytes, 0, len, "UTF-8"));}System.out.println("server2 get Message:" + sb);OutputStream outputStream = socket.getOutputStream();outputStream.write("我是服務(wù)器".getBytes(StandardCharsets.UTF_8));inputStream.close();outputStream.close();socket.close();serverSocket.close();} catch (IOException e) {e.printStackTrace();}}
}

多次接收消息

客戶端
package Scoket.client;import java.io.OutputStream;
import java.net.Socket;public class Client3 {public static void main(String args[]) throws Exception {// 要連接的服務(wù)端IP地址和端口String host = "127.0.0.1";int port = 8444;// 與服務(wù)端建立連接Socket socket = new Socket(host, port);// 建立連接后獲得輸出流OutputStream outputStream = socket.getOutputStream();String message = "你好  yiwangzhibujian";//首先需要計算得知消息的長度byte[] sendBytes = message.getBytes("UTF-8");//然后將消息的長度優(yōu)先發(fā)送出去outputStream.write(sendBytes.length >>8);outputStream.write(sendBytes.length);//然后將消息再次發(fā)送出去outputStream.write(sendBytes);outputStream.flush();//==========此處重復發(fā)送一次,實際項目中為多個命名,此處只為展示用法message = "第二條消息";sendBytes = message.getBytes("UTF-8");outputStream.write(sendBytes.length >>8);outputStream.write(sendBytes.length);outputStream.write(sendBytes);outputStream.flush();//==========此處重復發(fā)送一次,實際項目中為多個命名,此處只為展示用法message = "the third message!";sendBytes = message.getBytes("UTF-8");outputStream.write(sendBytes.length >>8);outputStream.write(sendBytes.length);outputStream.write(sendBytes);    outputStream.close();socket.close();}
}
服務(wù)端
package Scoket.client;import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;public class Server3 {public static void main(String[] args) {try {int port = 8444;ServerSocket serverSocket = new ServerSocket(port);Socket accept = serverSocket.accept();InputStream inputStream = accept.getInputStream();byte[] bytes;while (true){int first = inputStream.read();if(first == -1){break;}int second = inputStream.read();int len = (first << 8) +second;bytes = new byte[len];inputStream.read(bytes);System.out.println("Server3 get message:" + new String(bytes, "UTF-8"));}} catch (IOException e) {e.printStackTrace();}}
}

Socket寫法的問題

上面的代碼有些很大的問題
1、阻塞式 I/O: 這是最大的缺點之一。在 accept()、readLine() 等方法調(diào)用時,程序會被阻塞,等待客戶端連接或數(shù)據(jù)到來。這可能導致服務(wù)器在處理多個客戶端時性能下降。
2、單線程處理: 服務(wù)器采用單線程處理客戶端連接。這意味著一次只能處理一個客戶端連接,如果有大量的客戶端同時連接,性能會受到影響。
3、不適用于高并發(fā): 由于采用單線程處理方式,不適合高并發(fā)環(huán)境。在高并發(fā)情況下,建議考慮使用多線程或異步 I/O 模型。
4、異常處理不足: 缺少一些異常處理,例如,在 accept()、readLine() 中可能會拋出異常,而在示例中并未捕獲和處理這些異常。
針對1、2可以采用BIO方式
針對1、2、3可以采用NIO
接下來將會優(yōu)化代碼分別介紹BIO和NIO

BIO

簡單流程

服務(wù)器啟動一個ServerSocket。
客戶端啟動一個Socket對服務(wù)器進行通信,默認情況下,服務(wù)器端需要對每一個客戶端建立一個線程與之通信。
客戶端發(fā)出請求后,先咨詢服務(wù)器是否有線程相應,如果沒有則會等待,或者被拒絕。
如果有響應,客戶端線程會等待請求結(jié)束后,再繼續(xù)執(zhí)行。

BIO寫法

客戶端
package Scoket.client;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;public class BIOClient{public static void main(String[] args) {try {Socket socket = new Socket("127.0.0.1", 6666);OutputStream outputStream = socket.getOutputStream();outputStream.write("hi, i am client".getBytes(StandardCharsets.UTF_8));outputStream.flush();socket.close();} catch (IOException e) {e.printStackTrace();}}
}
服務(wù)端

轉(zhuǎn)自:https://juejin.cn/post/6924670437867651080

package Scoket.client;import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BIOServer {public static void main(String[] args) throws IOException {// 創(chuàng)建線程池ExecutorService executorService = Executors.newCachedThreadPool();// 創(chuàng)建ServerSocket并且監(jiān)聽6666端口ServerSocket serverSocket = new ServerSocket(6666);while (true) {// 監(jiān)聽---一直等待客戶端連接Socket socket = serverSocket.accept();// 連接來了之后,啟用一個線程去執(zhí)行里面的方法executorService.execute(() -> {try {// 獲取客戶端發(fā)送過來的輸入流InputStream inputStream = socket.getInputStream();byte[] bytes = new byte[1024];int read = inputStream.read(bytes);// 讀取發(fā)送過來的信息并打印if (read != -1) {System.out.println(new String(bytes, 0, read));}} catch (IOException e) {e.printStackTrace();} finally {// 斷開通訊try {socket.close();} catch (IOException e) {e.printStackTrace();}}});}}
}

BIO的問題

上述寫法主要是在服務(wù)端接受到一個客戶端連接時,就開啟一個線程,然后新建一個連接專門處理這個服務(wù)
可以看下accept代碼

    public Socket accept() throws IOException {if (this.isClosed()) {throw new SocketException("Socket is closed");} else if (!this.isBound()) {throw new SocketException("Socket is not bound yet");} else {Socket s = new Socket((SocketImpl)null);this.implAccept(s);return s;}}

可以看到,每次accept就會新建一個Socket
因此會有如下問題:
每個請求都需要創(chuàng)建獨立的線程,與對應的客戶端進行數(shù)據(jù)讀,業(yè)務(wù)處理,然后再數(shù)據(jù)寫。
當并發(fā)數(shù)較大時,需要創(chuàng)建大量的線程來處理連接,系統(tǒng)資源占用較大。
連接建立后,如果當前線程暫時沒有數(shù)據(jù)可讀,則線程就阻塞在讀操作上,造成線程資源浪費。

基于上面的問題產(chǎn)生了NIO

NIO

簡述

Java NIO全稱java non-blocking IO,是指JDK提供的新API。從JDK1.4開始,提供了一系列改進的輸入/輸出的新特性,被統(tǒng)稱為NIO(所以也可稱為New IO),是同步非阻塞的。
NIO相關(guān)類都被放在java.nio包及子包下,并且對原java.io包中的很多類進行改寫。
NIO有三大核心部分:
Channel(通道)
Buffer(緩沖區(qū))
Selector(選擇器)
NIO是面向緩沖區(qū)的。數(shù)據(jù)讀取到一個它的稍后處理的緩沖區(qū),需要時可以在緩沖區(qū)中前后移動,這就增加了處理過程中的靈活性,使用它可以提供非阻塞式的高伸縮性網(wǎng)絡(luò)。
Java NIO的非阻塞模式,是一個線程從某通道發(fā)送請求或者讀取數(shù)據(jù),但是它僅能得到目前可用的數(shù)據(jù),如果目前沒有數(shù)據(jù)可用時,就什么都不會獲取,而不是保持線程阻塞,所以直至數(shù)據(jù)變得可以讀取之前,該線程可以繼續(xù)做其他的事情。非阻塞寫也是如此,一個線程請求寫入一些數(shù)據(jù)到某通道,但不需要等待它完全寫入,這個線程同時可以去做別的事情。
通俗理解:NIO是可以做到用一個線程來處理多個操作的。假設(shè)有10000個請求過來,根據(jù)實際情況,可以分配50或者100個線程來處理。不像之前的阻塞IO那樣,非得分配10000個。
HTTP2.0使用了多路復用的技術(shù),做到了同一個連接并發(fā)處理多個請求,而且并發(fā)請求的數(shù)量比HTTP1.1大了好幾個數(shù)量級。
在這里插入圖片描述

Buffer

Buffer(緩沖區(qū)):緩沖區(qū)本質(zhì)上是一個可以讀寫數(shù)據(jù)的內(nèi)存塊,可以理解成是一個容器對象(含數(shù)組),該對象提供了一組方法,可以更輕松的使用內(nèi)存塊,緩沖區(qū)對象內(nèi)置了一些機制,能夠跟蹤和記錄緩沖區(qū)的狀態(tài)變化情況。Channel提供從文件、網(wǎng)絡(luò)讀取數(shù)據(jù)的渠道,但是讀取或?qū)懭氲臄?shù)據(jù)都必須經(jīng)由Buffer。
在使用Buffer進行數(shù)據(jù)讀寫的時候,主要是通過底層的這個數(shù)組來儲存數(shù)據(jù),但是具體的控制數(shù)據(jù)讀寫,是通過父類Buffer中的以下參數(shù)來控制的:

屬性描述
Capacity容量,即可以容納的最大數(shù)據(jù)量。在緩沖區(qū)被創(chuàng)建時被確定并且不能改變
Limit示緩沖區(qū)的當前終點,不能對緩沖區(qū)超過limit的位置進行讀寫操作,且limit是可以修改的
Position位置,下一個要被讀/寫的元素的索引,每次讀寫緩沖區(qū)數(shù)據(jù)時都會改變position的值,為下次讀寫做準備
Mark標記

一共有7個類直接繼承了Buffer類,這7個子類分別是除了boolean外的其他7中數(shù)據(jù)類型的Buffer類。
在這七個子類中,都有一個相應數(shù)據(jù)類型的數(shù)組,比如IntBuffer中就有一個int類型的數(shù)組:
final int[] hb;
在ByteBuffer類中就有一個byte類型的數(shù)組:
final byte[] hb;
實例:

package Scoket.client;import java.nio.IntBuffer;public class Buffer {public static void main(String[] args) {// 創(chuàng)建一個IntBuffer對象實例,分配容量為5IntBuffer buffer = IntBuffer.allocate(5);for (int i = 0; i < buffer.capacity(); i++) {// 每次循環(huán)為buffer塞一個int類型的數(shù)值,經(jīng)過5次循環(huán)后,buffer中應該有0、2、4、6、8這5個數(shù)buffer.put(i * 2);}// 當要將buffer從寫入轉(zhuǎn)換到讀取的時候,需要調(diào)用flip()方法// flip()方法是將limit指向position的位置,并且再將position置0// 表示從頭再讀到調(diào)用flip()方法的地方buffer.flip();// hasRemaining()方法表示是否還有剩余的元素可讀取// 里面是通過position < limit判斷是否有剩余的元素while (buffer.hasRemaining()) {System.out.println(buffer.get());}// 這時將position的位置設(shè)置成1,limit的位置設(shè)置成4buffer.position(1);buffer.limit(4);// 因為不能讀取超過limit的元素,并且從position位置開始讀取,所以這里將會輸出2、4、6while (buffer.hasRemaining()) {System.out.println(buffer.get());}}
}

Channel(通道)

NIO的通道類似于流,但兩者之間有所區(qū)別:
通道可以同時進行讀寫,而流只能讀或者只能寫
通道可以實現(xiàn)異步讀寫數(shù)據(jù)
通道可以從緩沖區(qū)讀取數(shù)據(jù),也可以寫數(shù)據(jù)到緩沖區(qū)
BIO的stream是單向的,例如FileInputStream對象只能進行讀取數(shù)據(jù)的操作,而NIO中的通道(Channel)是雙向的,可以讀操作,也可以寫操作。
Channel在NIO中是一個接口。
常用的Channel類有:FileChannel、DatagramChannel、ServerSocketChannel、SocketChannel。FileChannel用于文件的數(shù)據(jù)讀寫,DatagramChannel用于UDP的數(shù)據(jù)讀寫,ServerSocketChannel和SocketChannel用于TCP的數(shù)據(jù)讀寫。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;public class Channel {public static void main(String[] args) throws Exception {// 從桌面上隨機取一張圖片進行復制操作// 獲取原圖片和被復制圖片路徑的流FileInputStream fileInputStream = new FileInputStream("/src/main/resources/img.png");FileOutputStream fileOutputStream = new FileOutputStream("/src/main/resources/img_1.png");// 通過流的getChannel()方法獲取兩個通道FileChannel fileInputStreamChannel = fileInputStream.getChannel();FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();// 創(chuàng)建一個字節(jié)類型的緩沖區(qū),并為其分配1024長度ByteBuffer byteBuffer = ByteBuffer.allocate(1024);// 每次讀取圖片的字節(jié)到緩沖區(qū),當讀返回-1時,表示讀完了while (fileInputStreamChannel.read(byteBuffer) > -1) {// 調(diào)用flip()方法,從讀的狀態(tài)變?yōu)閷懙臓顟B(tài)byteBuffer.flip();// 復制,將緩沖區(qū)中的數(shù)據(jù)寫入到管道中fileOutputStreamChannel.write(byteBuffer);// 將緩沖區(qū)清空,以便于下一次讀取byteBuffer.clear();}// 關(guān)閉Closeable對象fileOutputStreamChannel.close();fileInputStreamChannel.close();fileOutputStream.close();fileInputStream.close();}
}

Selector(選擇器)

基本介紹

Java的NIO,用非阻塞的IO方式??梢杂靡粋€線程,處理多個的客戶端連接,就會使用到Selector(選擇器)。
Selector能夠檢測多個注冊的通道上是否有事件發(fā)生,如果有事件發(fā)生,便獲取時間然后針對每個事件進行相應的處理。這樣就可以只用一個單線程去管理多個通道,也就是管理多個連接和請求。
只有在連接通道真正有讀寫事件發(fā)生時,才會進行讀寫,就大大地減少了系統(tǒng)開銷,并且不必為每一個連接都創(chuàng)建一個線程,不用去維護多個線程。避免了多個線程之間的上下文切換導致的開銷
SelectionKey為Selector中,有一個Channel注冊了,就會生成一個SelectionKey對象,在同步非阻塞中,Selector可以通過SelectionKey找到相應的Channel并處理。
SelectionKey在Selector和Channel的注冊關(guān)系中一共分為四種:

Int OP_ACCEPT:有新的網(wǎng)絡(luò)連接可以accept,值為16(1<<4)
int OP_CONNECT:代表連接已經(jīng)建立,值為8(1<<3)
int OP_WRITE:代表寫操作,值為4(1<<2)
int OP_READ:代表讀操作,值為1(1<<0)

使用實例

客戶端:

package Scoket.client;import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;public class NioClient {public static void main(String[] args) throws IOException {// 連接服務(wù)器SocketChannel socketChannel = SocketChannel.open();socketChannel.connect(new InetSocketAddress("localhost", 6443));// 發(fā)送數(shù)據(jù)String message = "Hello, Server!";ByteBuffer buffer = ByteBuffer.wrap(message.getBytes("UTF-8"));socketChannel.write(buffer);System.out.println("Sent to server: " + message);// 關(guān)閉連接socketChannel.close();}
}

服務(wù)端

package Scoket.client;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.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;public class NioServer {public static void main(String[] args) throws IOException {// 打開 SelectorSelector selector = Selector.open();// 打開 ServerSocketChannel,監(jiān)聽客戶端連接ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();serverSocketChannel.bind(new InetSocketAddress(6443));// 設(shè)置為非阻塞模式serverSocketChannel.configureBlocking(false);// 注冊接受連接事件serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);System.out.println("Server listening on port 6443");while (true) {// 阻塞直到有就緒事件發(fā)生int readyChannels = selector.select();if (readyChannels == 0) {continue;}// 獲取就緒事件的 SelectionKey 集合Set<SelectionKey> selectedKeys = selector.selectedKeys();Iterator<SelectionKey> keyIterator = selectedKeys.iterator();while (keyIterator.hasNext()) {SelectionKey key = keyIterator.next();if (key.isAcceptable()) {// 有新的連接handleAccept(key, selector);} else if (key.isReadable()) {// 有數(shù)據(jù)可讀handleRead(key);}keyIterator.remove();}}}private static void handleAccept(SelectionKey key, Selector selector) throws IOException {ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();SocketChannel socketChannel = serverSocketChannel.accept();socketChannel.configureBlocking(false);// 注冊讀事件socketChannel.register(selector, SelectionKey.OP_READ);System.out.println("Accepted connection from: " + socketChannel.getRemoteAddress());}private static void handleRead(SelectionKey key) throws IOException {SocketChannel socketChannel = (SocketChannel) key.channel();ByteBuffer buffer = ByteBuffer.allocate(1024);int bytesRead = socketChannel.read(buffer);if (bytesRead > 0) {buffer.flip();byte[] data = new byte[bytesRead];buffer.get(data);System.out.println("Received from client: " + new String(data, "UTF-8"));// 在這里可以添加業(yè)務(wù)邏輯,然后將響應數(shù)據(jù)寫入到 SocketChannel// ...// 關(guān)閉連接socketChannel.close();}}
}

參考源:
https://zhuanlan.zhihu.com/p/462497498
https://blog.csdn.net/a78270528/article/details/80318571
https://juejin.cn/post/6924670437867651080
https://juejin.cn/post/6925046428213608456

http://www.risenshineclean.com/news/33354.html

相關(guān)文章:

  • 自己做網(wǎng)站排名好嗎熱搜榜排名今日
  • 網(wǎng)站上的圖分辨率做多少搜狗站長工具
  • 大型門戶網(wǎng)站建設(shè)需要哪些技術(shù)百度號碼認證平臺官網(wǎng)首頁
  • 西安做網(wǎng)站需要多少錢京東seo搜索優(yōu)化
  • 站長網(wǎng)seo綜合查詢工具百度托管公司
  • 做網(wǎng)站優(yōu)化有用嗎百度廣告公司聯(lián)系方式
  • 電腦手機網(wǎng)站制作網(wǎng)站免費優(yōu)化
  • ps做網(wǎng)站的流程2023年8月份新冠
  • 專業(yè)網(wǎng)絡(luò)推廣公司排名北京推廣優(yōu)化經(jīng)理
  • 成都網(wǎng)站注冊域名注冊后如何建網(wǎng)站
  • 點擊未來網(wǎng)站建設(shè)游戲代理
  • 新手做網(wǎng)站的詳細步驟網(wǎng)站友鏈
  • 北京做網(wǎng)站定制價格seo診斷服務(wù)
  • 1688運營自學全套教程seo網(wǎng)站推廣工具
  • 蘇州吳中區(qū)做網(wǎng)站新東方教育培訓機構(gòu)官網(wǎng)
  • wordpress恢復分類目錄seo營銷論文
  • 自動化東莞網(wǎng)站建設(shè)北京疫情最新消息
  • 網(wǎng)站開發(fā)視頻壓縮上傳seo資源
  • 旅游網(wǎng)站在提高用戶體驗方面應做哪些工作長春網(wǎng)站建設(shè)制作
  • 做吃的教程網(wǎng)站品牌整合營銷方案
  • 典型網(wǎng)站開發(fā)的一般流程推廣app是什么工作
  • 好看網(wǎng)站手機版批量查詢權(quán)重
  • 網(wǎng)站建設(shè)php帶數(shù)據(jù)庫模板網(wǎng)絡(luò)安全
  • 如何做網(wǎng)站鏡像百度鏈接提交入口
  • 手機網(wǎng)站有哪些類型成都網(wǎng)絡(luò)推廣
  • 網(wǎng)站建設(shè) 開發(fā)網(wǎng)站代碼百度網(wǎng)盤官網(wǎng)
  • 怎么用家里的電腦做網(wǎng)站服務(wù)器上海seo公司排名
  • 學校網(wǎng)站建設(shè)的優(yōu)勢和不足成人用品推廣網(wǎng)頁
  • 陽春網(wǎng)站制作寧波網(wǎng)站建設(shè)推廣平臺
  • 個人網(wǎng)站 備案 廣告seo搜索引擎優(yōu)化價格