当前位置:首页 > CN2资讯 > 正文内容

ServerSocketChannel服务器tcp服务器

1天前CN2资讯


NIO服务器,代码如下:

package com.nio; import java.io.IOException; import .InetSocketAddress; import .ServerSocket; 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 { private Selector selector; private static final int BLOCK = 4096; private final ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK); private final ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK); public NIOServer(int port) { try { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); ServerSocket serverSocket = serverSocketChannel.socket(); serverSocket.bind(new InetSocketAddress(port)); selector = Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); } catch (IOException e) { e.printStackTrace(); } } public void listen() throws IOException { while (true) { selector.select(); Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey selectionKey = iterator.next(); iterator.remove(); handleKey(selectionKey); } } } private StringBuilder recv = new StringBuilder(); private void handleKey(SelectionKey selectionKey) throws IOException { ServerSocketChannel server = null; SocketChannel client = null; String receiveText; String sendText; StringBuilder send = new StringBuilder(); int count = 0; if (selectionKey.isAcceptable()) { server = (ServerSocketChannel) selectionKey.channel(); client = server.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_READ); } else if (selectionKey.isReadable()) { recv.setLength(0); client = (SocketChannel) selectionKey.channel(); System.out.print("client:"); while (true) { receivebuffer.clear(); count = client.read(receivebuffer); if (count == -1) { break; } receiveText = new String(receivebuffer.array(), 0, count); recv.append(receiveText); if ("\r\n".equals(receiveText)) { break; } System.out.print(receiveText); } System.out.println(); client.register(selector, SelectionKey.OP_WRITE); } else if (selectionKey.isWritable()) { sendbuffer.clear(); client = (SocketChannel) selectionKey.channel(); sendText = "server:"; send.append(sendText); send.append(recv); sendbuffer.put(send.toString().getBytes()); sendbuffer.flip(); client.write(sendbuffer); client.register(selector, SelectionKey.OP_READ); } } public static void main(String[] args) throws IOException { int port = 8888; NIOServer server = new NIOServer(port); server.listen(); } }



在客户端用telnet或者nc来连接

telnet 127.0.0.1 8888


    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/21194.html

    分享给朋友:

    “ServerSocketChannel服务器tcp服务器” 的相关文章

    cn2排列公式怎么展开?原来排列组合还可以这么学!

    排列组合作为数学中一个重要的分支,在日常生活和实际问题中都有着广泛的应用。无论是计算概率、解决实际问题,还是在统计学中分析数据,排列组合都是不可或缺的工具。而在排列组合的核心公式中,C(n,2)是一个非常基础但又极其重要的公式。C(n,2)排列公式到底怎么展开?它背后又有哪些深层次的数学原理呢?让我...

    如何高效管理Ubuntu服务器:从基础到高级的全面指南

    管理Ubuntu服务器是一个需要掌握多种技能的任务。从选择合适的Linux发行版到系统初始化,再到账号和权限管理,每一个环节都至关重要。以下是一些基础的管理技巧,帮助你更好地配置和管理Ubuntu服务器。 1.1 选择合适的Linux发行版 在国内,常用的Linux发行版有CentOS、Ubuntu...

    JustHost优惠码大揭秘:节省开支的绝佳办法

    JustHost概述 我对JustHost的了解始于它的多样化主机产品和用户友好的服务。JustHost成立于2006年,作为一家俄罗斯主机商,它提供虚拟主机、VPS服务器以及独立服务器,是一个值得关注的选择。JustHost不仅拥有丰富的技术背景,还致力于为用户提供高性价比的服务,这让我对它充满了...

    宝塔面板PHP扩展新增指南:提升网站性能的实用技巧

    在日常网站管理和服务器配置中,宝塔面板的出现让这一切变得更为简单直观。作为一个流行的服务器控制面板,宝塔面板以其用户友好的界面和丰富的功能备受欢迎。对于没有技术背景的用户来说,它提供了极大的便利,而对于开发者来说,宝塔也能高效管理复杂的服务器配置。 宝塔面板不仅支持多种服务器环境,还能够轻松管理数据...

    阿里云国际站:轻松注册与支付方式全解析

    什么是阿里云国际站? 在这个科技迅速发展的时代,云计算已经成为许多企业和个人开展业务的重要基础设施。阿里云国际站便是一处全球化的云计算服务平台,旨在为世界各地的用户提供高效、灵活和安全的云计算服务。它不仅支持多种功能,还具备强大的全球基础设施,能够满足不同用户的需求。 阿里云国际站被设定为一个面向全...

    甲骨文云的永久免费服务:开发者的理想选择

    在现代云计算的环境中,甲骨文云(Oracle Cloud)作为一种强有力的云计算服务,凭借其永久免费服务吸引了许多用户。回想我初次接触甲骨文云时,正是被它提供的多种Always Free服务所吸引,比如我可以免费使用2个实例和20GB的存储空间。这让我在学习和开发上有了更加广阔的可能性,不用担心一开...