Java怎么让socket服务一直运行?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Java怎么让socket服务一直运行?

李博 bluemind 2019-04-28 17:38:16 3506

本问题来自云栖社区【阿里Java技术进阶2群】。https://yq.aliyun.com/articles/690084 点击链接欢迎加入社区大社群。

Java
分享到
取消 提交回答
全部回答(9)
  • 1156606323420497
    2020-03-26 23:55:02

    用while()循环不断接受和发送,要用线程啊记得。。。然后添加事件响应 关闭程序就调用socket.close()

    0 0
  • galaxystar
    2020-03-18 17:00:30

    非守护线程

    0 0
  • huc_逆天
    2019-11-12 17:27:21

    基本上来说的,socker连接,服务始终运行,是都通while(true)

    0 0
  • Joyven
    2019-11-12 17:17:06

    不管是BIO还是NIO,其实都是通过轮训的方式来实现socket服务的。下面几个demo或许有助于你:

    • BIO
    public class BIODemo {
        public static void main(String[] args) throws IOException {
            ServerSocket serverSocket = new ServerSocket();
            serverSocket.bind(new InetSocketAddress("0.0.0.0", 8888), 50);
            Socket socket;
            while ((socket = serverSocket.accept()) != null) {
                InputStream is = socket.getInputStream();
                byte[] data = new byte[1024];
                is.read(data);
    
                System.out.println(new String(data, UTF_8));
                OutputStream out = socket.getOutputStream();
                out.write(data);
                socket.close();
            }
        }
    }
    
    • NIO
    public class NIODemo {
        public static void main(String[] args) throws IOException {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.bind(new InetSocketAddress("0.0.0.0", 8888), 50);
            serverSocketChannel.configureBlocking(false);
            Selector selector = Selector.open();
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    
            while (true) {
                selector.select();
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    if (!key.isValid()) {
                        continue;
                    }
    
                    if (key.isAcceptable()) {
                        ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
                        SocketChannel clientChannel = serverChannel.accept();
                        clientChannel.configureBlocking(false);
                        clientChannel.register(selector, SelectionKey.OP_READ);
                    } else if (key.isReadable()) {
                        ByteBuffer buffer = ByteBuffer.wrap(new byte[1024]);
                        SocketChannel clientChannel = (SocketChannel) key.channel();
                        int read = clientChannel.read(buffer);
    
                        if (read == -1) {
                            key.cancel();
                            clientChannel.close();
                        } else {
                            buffer.flip();
                            clientChannel.write(buffer);
                        }
                    }
                }
                iterator.remove();
            }
        }
    }
    

    如果你需要更详细的更深入的了解,可以参考我的这篇文章 JAVA中BIO、NIO、AIO的分析理解 https://developer.aliyun.com/article/726698?spm=a2c6h.13148508.0.0.1d844f0eaWNdWj

    0 0
  • 有头发的程序猿
    2019-10-30 16:10:24

    不断的轮询请求,通过accept()方法实现。

    0 0
滑动查看更多
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程