tcp nio basics

  • S1_315834
    了解作者
  • 2.7KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-15 10:53
    上传日期
NULL 博文链接:https://pelli.iteye.com/blog/2398349
n-sock.zip
  • NServer.java
    4.5KB
  • NClient.java
    3.4KB
内容介绍
package sock; import java.io.*; import java.net.*; import java.nio.*; import java.nio.channels.*; import java.util.*; /** * 接收input => 发送hello input * * <p>Note:Read,write 时都有可能出错例如“IOException: 远程主机强迫关闭了一个现有的连接”,这里没有特殊处理。 */ public class NServer { static class Attach { String name; String output; Attach(String name) { this.name = name; } static String nameOf(SelectionKey key) { Attach a = (Attach) key.attachment(); return a.name; } static void output(SelectionKey key, String output) { Attach a = (Attach) key.attachment(); a.output = output; } static String outputOf(SelectionKey key) { Attach a = (Attach) key.attachment(); return a.output; } } //class Attach public static void main(String[] args) throws Exception { Selector selector = Selector.open(); ServerSocketChannel serverCh = ServerSocketChannel.open(); serverCh.bind(new InetSocketAddress(8888)); serverCh.configureBlocking(false); serverCh.register(selector, SelectionKey.OP_ACCEPT); int chCount = 0; ByteBuffer buf = ByteBuffer.allocate(1024); boolean exit = false; for(; !exit; ) { selector.select(); Iterator<SelectionKey> ite = selector.selectedKeys().iterator(); for (; ite.hasNext() ;) { SelectionKey key = ite.next(); if(key.isAcceptable()) { SocketChannel ch = ((ServerSocketChannel) key.channel()).accept(); ch.configureBlocking(false); SelectionKey key2 = ch.register(selector, SelectionKey.OP_READ); key2.attach(new Attach("Ch-" + ++chCount)); ite.remove(); } else if(key.isReadable()) { SocketChannel ch = (SocketChannel) key.channel(); buf.position(0); int len; try {len = ch.read(buf); } //直接强关telnet客户端 会返回-1,无异常。 catch (IOException e) { //直接强关NClient 会异常“远程主机强迫关闭了一个现有的连接” len = -1; } if(len == -1) { System.out.println(Attach.nameOf(key) + " Read: EOF"); key.interestOps(key.interestOps() & ~SelectionKey.OP_READ); } else if(len > 0) { String input = new String(buf.array(), 0, len).trim(); // trailing \r\n System.out.println(Attach.nameOf(key) + " Read: " + input); if("close".equals(input)) { System.out.println(Attach.nameOf(key) + " Close"); ch.close(); } else if("exit".equals(input)) { System.out.println(Attach.nameOf(key) + " Exit"); ch.close(); exit = true; break; } else { System.out.println(Attach.nameOf(key) + " will Write: " + input); key.interestOps(key.interestOps() | SelectionKey.OP_WRITE); Attach.output(key, "Hello " + input); } } else if(len == 0) { System.out.println(Attach.nameOf(key) + " Read: len=0"); } ite.remove(); } else if(key.isWritable()) { SocketChannel ch = (SocketChannel) key.channel(); String output = Attach.outputOf(key); System.out.println(Attach.nameOf(key) + " Write: " + output); ch.write(ByteBuffer.wrap((output + "\n").getBytes())); key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE); ite.remove(); } } //for selected-keys } //for System.out.println("Close selector .."); selector.close(); System.out.println("Close serverCh .."); serverCh.close(); } }
评论
    相关推荐
    • CasseBrique:https
      CasseBrique:https
    • 使用socket post 数据到httphttps
      使用socket post 数据到httphttps
    • nodejs http-server开启https的证书
      https证书文件,nodejs http-server开启https的证书,可以直接在本地启动https的协议,方便部署和使用。
    • http https 切换
      http https切换代码,能够实现网站在传输信息的过程中不易遭人盗取
    • Http2Https-crx插件
      将书签中的http转换为https 害怕用https书签替换旧的http书签? 随着许多网站迁移到https,旧的http书签使星标图像为空。 搜索并转换为https! ****************************************************** ***********...
    • LocomotiveCMS https to http-crx插件
      语言:English 将登录网址从https替换为http以进行开发 此扩展程序供使用机车cms开发应用程序时供个人使用。 为了易于在开发模式下使用,此扩展程序将https登录网址替换为http
    • http:https的镜像
      框架HTTP
    • https镜像
      scp镜 SCP基金会部分界面https //
    • CookieIsolator:分离 HTTPHTTPS cookie
      松散隔离:HTTP cookie 只能在 HTTP 通道中发送,而 HTTPS cookie 可以在 HTTP(不带安全标志)和 HTTPS 通道中发送。 严格隔离:HTTP cookie 和 HTTPS cookie 只能分别在 HTTPHTTPS 通道中发送。 Ext Secure...
    • https协议
      NULL 博文链接:https://willwen.iteye.com/blog/1988199