SocketChannel的read值为0
2016-01-10 00:09
211 查看
读取的时候按照以下方法,记得要buf.clear()才能留有空间接收-1
while ((numBytesRead = socketChannel.read(buf)) != -1) { if (numBytesRead == 0) { // 如果没有数据,则稍微等待一下 try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } continue; } // 转到最开始 buf.flip(); /*处理buf中的数据*/ // 也可以转化为字符串,不过需要借助第三个变量了。 // buf.get(buff, 0, numBytesRead); // System.out.println(new String(buff, 0, numBytesRead, "UTF-8")); buf.clear(); }
相关文章推荐
- python2.7---查询信息代码
- hibernate提供的5种检索数据方式
- Linux下XAMPP装完之后,Navicat无法连上数据库的问题的解决 注意'mypassword'是当前的mysql登录密码
- Android Studio中导入依赖库的方法
- 011: Errors and Exceptions
- JDBC简易入门-各层的写法-02
- mac学习笔记:brew 安装nginx
- UIView的一些知识
- tomcat启动没有8080端口
- Centos7最小化终端命令行安装VMware-tools.pl
- Swift学习笔记1
- Linux Kernel 4.0 RC5 发布!
- Ubuntu Touch 升级即将来袭!
- Linux 下无损图片压缩小工具介绍
- 一张图看尽 Linux 内核运行原理
- Sourcegraph:开发者的福音
- python轻松查到删除自己的微信好友
- 详解Python发送邮件实例
- PHP生成各种常见验证码和Ajax验证过程
- PHP 设计模式系列之 specification规格模式