java flash tcp字节流通信(四)-java 服务端(粘包/半包处理)
来源:互联网 发布:ubuntu怎么读u盘 编辑:程序博客网 时间:2024/05/17 00:01
package com.net.test;
import java.io.IOException;
import java.net.InetSocketAddress;
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;
import com.net.tcp.DataCache;
import com.net.tcp.DataPack;
import com.net.tcp.StreamObjectManager;
/**
*
* nio通信服务端
*
*/
public class NIOServer {
private ServerSocketChannel serverSokcetChannel;
private Selector selector;
public NIOServer(int port) throws Exception{
serverSokcetChannel = ServerSocketChannel.open();
serverSokcetChannel.configureBlocking(false);
serverSokcetChannel.socket().bind(new InetSocketAddress(port));
selector = Selector.open();
serverSokcetChannel.register(selector, SelectionKey.OP_ACCEPT);
listen();
}
public void listen(){
Set<SelectionKey> keys = null;
Iterator<SelectionKey> keyIt = null;
SelectionKey key = null;
while(true){
try {
selector.select();
keys = selector.selectedKeys();
keyIt = keys.iterator();
while(keyIt.hasNext()){
key = keyIt.next();
keyIt.remove();
dealKey(key);
}
} catch (Exception e) {
e.printStackTrace();
if(key != null)
closeChannel((SocketChannel)key.channel());
}
}
}
public void dealKey(SelectionKey key) throws Exception{
if(key.isAcceptable()){
ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
SocketChannel channel = serverChannel.accept();
channel.configureBlocking(false);
DataCache[] caches = {new DataCache(1024), new DataCache(0)};
channel.register(selector, SelectionKey.OP_READ, caches);
}else if(key.isReadable()){
SocketChannel channel = (SocketChannel)key.channel();
DataCache[] cache = (DataCache[])key.attachment();
if(cache[0].read(channel) > 0){
DataPack pack = null;
boolean sign = false;
while((pack = cache[0].poll()) != null){
cache[1].push(pack);
sign = true;
}
if(sign){
key.interestOps(SelectionKey.OP_WRITE);
}
}else{
closeChannel(channel);
}
// ByteBuffer buffer = ByteBuffer.allocate(1024);
// if(channel.read(buffer) > 0){
// buffer.flip();
// int size = buffer.getInt();
// byte[] bytes = new byte[size];
// buffer.get(bytes);
// DataPack pack = new DataPack();
// pack.getContent().putBytes(bytes);
// pack.filp();
// Object object = pack.getObject();
// key.interestOps(SelectionKey.OP_WRITE);
// key.attach(object);
// }else{
// closeChannel(channel);
// }
}else if(key.isWritable()){
SocketChannel channel = (SocketChannel)key.channel();
DataCache[] cache = (DataCache[])key.attachment();
if(cache[1].write(channel)){
key.interestOps(SelectionKey.OP_READ);
}
// DataPack pack = new DataPack();
// pack.putObject(key.attachment(), DataType.TYPE_OBJECT);
// pack.filp();
// channel.write(pack.getHead().getContent());
// ByteBuffer buffer = pack.getContent().current(false);
// while(buffer != null){
// channel.write(buffer);
// buffer = pack.getContent().next(false);
// }
// key.interestOps(SelectionKey.OP_READ);
}
}
public void closeChannel(SocketChannel channel){
if(channel != null){
SelectionKey key = channel.keyFor(selector);
if(key != null){
key.cancel();
}
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws Exception{
StreamObjectManager.init("com.net.test.*");
new NIOServer(2345);
}
}
- java flash tcp字节流通信(四)-java 服务端(粘包/半包处理)
- java flash tcp字节流通信(四)-flash as3 客户端(粘包/半包处理)
- java flash tcp字节流通信(四)-flash as3 粘包/半包处理器(数据缓存)
- java flash tcp字节流通信(四)-java 粘包/半包处理器(数据缓存)
- java flash tcp字节流通信(二)-java 服务端
- java flash tcp字节流通信(二)-flash as3客户端
- java flash tcp字节流通信(一)-java 协议(1)
- java flash tcp字节流通信(一)-java 协议(3)
- java flash tcp字节流通信(一)-java 协议(4)
- java flash tcp字节流通信(一)-java 协议(5)
- java flash tcp字节流通信(一)-java 协议(2)
- java flash tcp字节流通信(一)-java 协议(6)
- java flash tcp字节流通信(一)-flash as3 协议(1)
- java flash tcp字节流通信(一)-flash as3 协议(2)
- java flash tcp字节流通信(一)-flash as3 协议(3)
- java flash tcp字节流通信(一)-flash as3 协议(4)
- java flash tcp字节流通信(一)-flash as3 协议(5)
- java flash tcp字节流通信(三)-java StreamObject方法生成器
- 位操作:十进制数转为二进制,八进制,十六进制
- printf的重定向与恢复
- 提升C语言内功的书——部分相关电子书在本人资源里
- 6配置环境参数
- db2 exists 和in联合使用优化方法
- java flash tcp字节流通信(四)-java 服务端(粘包/半包处理)
- 一个c指针问题的思考
- android SDK4.0源代码下载
- 数据统计计算的实现步骤
- datatable转json工具类
- Nand Flash原理分析与编程
- linux修改主机名的方法
- Android Dalvik虚拟机介绍
- 关于函数级链接