首页 类库下载 java类库 java NIO套接字编程

java NIO套接字编程

Oct 13, 2016 pm 03:27 PM

在java的套接字编程中,大部分一般使用阻塞IO套接字编程。套接字的读取和写入会阻塞(也就是说不管现在有没有写入/读出数据 调用read和write方法将会阻塞)。而NIO将I/O事件注册,当特定的注册I/O事件到达时会通知您。不需要轮询,也不需要创建大量的线程下面一个例子:下载

Server代码  

package simple.socket;  
  
import java.io.IOException;  
import java.net.InetSocketAddress;  
import java.net.ServerSocket;  
import java.nio.ByteBuffer;  
import java.nio.channels.*;  
import java.util.Iterator;  
import java.util.Set;  
  
/**  
 * Created by banxia on 16/10/12.  
 */  
public class UseNioSocket {  
  
  
    public static void main(String[] args) throws IOException {  
  
        // 创建一个selector 对象  
        Selector selector = Selector.open();  
  
        ServerSocketChannel ssc = ServerSocketChannel.open();  
        ssc.configureBlocking(false);  
        ssc.socket().bind(new InetSocketAddress(8888));  
        ssc.register(selector, SelectionKey.OP_ACCEPT);  
        ByteBuffer buffer = ByteBuffer.allocate(1024);  
  
        System.out.println("服务已经启动端口:" + 8888);  
  
        while (true) {  
  
            int selectNum = selector.select();  
            System.out.println("selectNum=" + selectNum);  
  
            if (selectNum <= 0) {  
                continue;  
            }  
            // 获取  
  
            Iterator<SelectionKey> it = selector.selectedKeys().iterator();  
  
            while (it.hasNext()) {  
  
                SelectionKey key = it.next();  
  
                if (key.readyOps() == SelectionKey.OP_ACCEPT) {  
                    // 接收 accept  
  
                    ServerSocketChannel channel = (ServerSocketChannel) key.channel();  
                    SocketChannel accept = channel.accept();  
                    accept.configureBlocking(false);  
                    System.out.println("接收客户端:" + accept.socket().getInetAddress().getHostAddress() + ":" +  accept.socket().getPort());  
                    accept.register(selector, SelectionKey.OP_READ);  
                    it.remove();  
  
  
                } else {  
  
                    SocketChannel sc = (SocketChannel) key.channel();  
                    System.out.println("接收客户端:" + sc.socket().getInetAddress().getHostAddress() + ":" + sc.socket().getPort() +"开始处理...");  
                    while (true) {  
                        buffer.clear();  
                        long l = sc.read(buffer);  
                        if (l <= 0) {  
                            break;  
                        }  
                        System.out.print(".");  
                        buffer.flip();  
                        sc.write(buffer);  
  
  
                    }  
                    System.out.print("\n");  
                    it.remove();  
                    System.out.println("接收客户端:" + sc.socket().getInetAddress().getHostAddress() + ":" + sc.socket().getPort() +"处理完成处理...");  
  
                }  
  
            }  
  
  
        }  
  
  
    }  
}
登录后复制


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)