nio tcp 释放_TCP NIO连续发送多条消息时,服务端接收时会将多条消息合并,请帮忙分析。...

服务端代码:

import java.io.IOException;

import java.net.InetSocketAddress;

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.nio.charset.Charset;

import java.util.Iterator;

public class TCPServerManager {

private static final int PORT = 8089;

private Charset charset = Charset.forName("UTF-8");

private Selector selector = null;

private ServerSocketChannel server = null;

public void start() throws IOException{

this.init();

new Thread(new ReceiveWorker()).start();

}

private void init() throws IOException{

InetSocketAddress address = null;

selector = Selector.open();

server = ServerSocketChannel.open();

address = new InetSocketAddress(PORT);

server.socket().bind(address);

server.configureBlocking(false);

server.register(selector, SelectionKey.OP_ACCEPT);

}

/**

* 接收消息线程

* @author luozejun

*

*/

class ReceiveWorker implements Runnable{

@Override

public void run() {

SelectionKey key = null;

while(true){

try {

if(selector.select() > 0 );

Iterator iterator = selector.selectedKeys().iterator();

while (iterator.hasNext()) {

key = iterator.next();

iterator.remove();

if(key.isAcceptable()){

SocketChannel sc = server.accept();

sc.configureBlocking(false);

sc.register(selector, SelectionKey.OP_READ);

key.interestOps(SelectionKey.OP_ACCEPT);

}

if(key.isReadable()){

handlerMessage(key);

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 消息处理

* @param key

*/

private void handlerMessage(SelectionKey key){

ByteBuffer buffer = ByteBuffer.allocate(8*1024);

SocketChannel channel = null;

StringBuilder content = new StringBuilder();

try{

channel = (SocketChannel)key.channel();

while(channel.read(buffer) > 0){

buffer.flip();

content.append(charset.decode(buffer));

}

System.out.println("接收:"+content);

}catch (Exception e) {

key.cancel();

if(channel !=null){

try {

channel.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

e.printStackTrace();

}

}

}

public static void main(String[] args) {

TCPServerManager manager = new TCPServerManager();

try {

manager.start();

System.out.println("server success");

} catch (IOException e) {

e.printStackTrace();

}

}

}

客户端发送代码

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.channels.SelectionKey;

import java.nio.channels.Selector;

import java.nio.channels.SocketChannel;

import java.nio.charset.Charset;

public class TCPClient {

private static final String HOST = "127.0.0.1";

private static final int PORT = 8089;

private Charset charset = Charset.forName("UTF-8");

private Selector selector = null;

private SocketChannel channel = null;

/**

* 初始化

* @throws IOException

*/

private void init() throws IOException{

InetSocketAddress address = null;

selector = Selector.open();

address = new InetSocketAddress(HOST,PORT);

channel = SocketChannel.open(address);

channel.configureBlocking(false);

channel.register(selector, SelectionKey.OP_READ);

}

/**

* 发送消息

* @param msgXml

* @throws IOException

*/

public void send(String msgXml) throws IOException{

channel.write(charset.encode(msgXml));

}

public static void main(String[] args) throws IOException {

TCPClient tcp = new TCPClient();

tcp.init();

tcp.send("您好啊");

tcp.send(" 您在哪里");

tcp.send(" 吃饭了没有");

tcp.send( " 还没有哦");

tcp.send( " 来我家吃吧");

//这个无作用,只是不让线程停掉,防止TCP断开

while(true){

//

}

}

}

客户端发送了5条消息,而服务端接收将发送的消息合并了。

服务端打印结果:

接收:您好啊 您在哪里

接收:吃饭了没有 还没有哦 来我家吃吧

因为需求是要求是批量发送消息,所以不希望线程等待。请各位一起分析下哪里需要调整。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值