NioSocket簡單復(fù)習(xí)

重要概念
NioSocket里面的三個重要概念:Buffer、Channel、Selector
使用步驟
使用NioSocket實現(xiàn)通信大概如以下步驟:
實現(xiàn)HTTP
創(chuàng)建HttpServer類作為程序的主要入口
public class HttpServer {
public static void main(String[] args) throws Exception{
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress((8080)));
serverSocketChannel.configureBlocking(false);
Selector selector = Selector.open();
// It must be ACCEPT, or it will throw exception
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while(true){
if (selector.select(3000) == 0){
continue;
}
Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator();
while (keyIter.hasNext()){
SelectionKey key = keyIter.next();
new Thread(new HttpHandler(key)).run();
keyIter.remove();
}
}
}
}
文章題目:Java使用NioSocket手動實現(xiàn)HTTP服務(wù)器-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://www.chinadenli.net/article46/dpcihg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站營銷、品牌網(wǎng)站制作、品牌網(wǎng)站設(shè)計、軟件開發(fā)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容