NioSocket簡單復習

重要概念
NioSocket里面的三個重要概念:Buffer、Channel、Selector
使用步驟
使用NioSocket實現通信大概如以下步驟:
實現HTTP
創建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手動實現HTTP服務器-創新互聯
文章網址:http://www.yijiale78.com/article46/dpcihg.html
成都網站建設公司_創新互聯,為您提供ChatGPT、網站營銷、品牌網站制作、品牌網站設計、軟件開發、外貿網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯