GithubHelp home page GithubHelp logo

javanet's Introduction

简介

javaNet适用于java网络编程,手写NIO,AIO服务端和客户端,以及netty的使用。客户端和服务端可以互相发送消息,服务端发送消息为发送给所有客户端。

java命令行使用

java命令行执行程序解决依赖外部jar包的问题

java命令行执行程序解决依赖外部jar包的问题
用java命令行直接执行程序,如果这个程序需要引用外部jar包。就不能单纯用java xx来执行

如果你的jar包和程序就在一个目录:
编译
javac -cp D:\yy\yy.jar,D\xx\xx.jar com.my.test.java
执行
java -cp D:\yy\yy.jar,D\xx\xx.jar com.my.test
但是往往我们依赖的包很多,要一个个填写估计就疯了。所有我们一般会把所有的外部依赖包都放在一个文件夹里,比如在D:\lib

编译 
javac -Djava.ext.dirs=D:\lib com.my.test.java
执行
java  -Djava.ext.dirs=D:\lib com.my.test
这个方法需要在jdk1.6以上支持
对于打包且依赖第三方库的class文件调试,命令行可以这么使用

参考资料

坑点参考

依赖

IO和NIO仅使用jdk1.8,没有使用第三方库。 httpserver和httpclient使用netty4.1.32

模块

  • MyNIOServer,用于NIO服务端
  • MyNIOClient,用于NIO客户端

知识点

  • Selector
  • SelectionKey
  • 判断客户端断开连接

client方异常退出时server会收到一个OP_READ,服务端收到后处理读操作触发异常"远程主机强迫关闭了一个现有的连接"

  • 判断服务端断开连接

server方异常退出时client会收到一个OP_READ,服务端收到后处理读操作触发异常"远程主机强迫关闭了一个现有的连接"

难点

异常断开连接后的读写操作都会导致异常,所以设计良好的异常处理很重要。本来在client和server中都有errMessage字段用于替代e.printStackTrace,实际操作上会漏掉,这要根据函数的返回值判断,而实际上对外暴露的接口并没有触发异常,而是内部执行导致了异常,对外是不可见的,还需要更好的设计展示。

写就绪相对有一点特殊,一般来说,不应该注册写事件。 写操作的就绪条件为底层缓冲区有空闲空间,而写缓冲区绝大部分时间都是有空闲空间的,所以当你注册写事件后,写操作一直是就绪的,选择处理线程全占用整个CPU资源,这也就是导致一直接收到OP_WRITE的原因。所以,只有当你确实有数据要写时再注册写操作,并在写完以后马上取消注册。

当有数据在写时,将数据写到缓冲区中,并注册写事件。

public void write(byte[] data) throws IOException {  
    writeBuffer.put(data);  
    key.interestOps(SelectionKey.OP_WRITE);
} 

注册写事件后,写操作就绪,这时将之前写入缓冲区的数据写入通道,并取消注册。

channel.write(writeBuffer);  
key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);  

javanet's People

Contributors

feifa168 avatar feitian168 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.