服务端代码:
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条消息,而服务端接收将发送的消息合并了。
服务端打印结果:
接收:您好啊 您在哪里
接收:吃饭了没有 还没有哦 来我家吃吧
因为需求是要求是批量发送消息,所以不希望线程等待。请各位一起分析下哪里需要调整。。