在本例中,将接收客户连接的操作单独由一个线程完成,把接收数据和发送数据的操作由另一个线程完成,这可以提高服务器的并发性能。
负责接收客户连线的线程按照阻塞模式工作,如果收到客户连接,就向selector注册读就绪和写就绪事件,否则进入阻塞状态,直到接收到了客户的连接。负责接收数据和发送数据的线程按照非阻塞模式工作,只有在读就绪和写就绪的事件发生时,才执行相应的接收数据和发送数据操作。
package com.test.socket.nio.thread2;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;
public class EchoServer{
private Selector selector = null;
private ServerSocketChannel serverSocketChannel = null;
private int port = 8000;
private Charset charset=Charset.forName("GBK");
public EchoServer()throws IOException{
selector = Selector.open();
serverSocketChannel= ServerSocketChannel.open();
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
System.out.println("服务器启动");
}
public void accept(){
for(;;){
try{
SocketChannel socketChannel = serverSocketChannel.accept();
System.out.println("接收到客户连接,来自:" +
socketChannel.socket().getInetAddress() +
":" + socketChannel.socket().getPort());
socketChannel.configureBlocking(false);
ByteBuffer buffer = ByteBuffer.allocate(1024);
synchronized(gate){
selector.wakeup();
socketChannel.register(selector,
SelectionKey.OP_READ |
SelectionKey.OP_WRITE, buffer);
}
}catch(IOException e){e.printStackTrace();}
}
}
private Object gate=new Object();
public void service() throws IOException{
for(;;){
synchronized(gate){}
int n = selector.select();
if(n==0)continue;
Set readyKeys = selector.selectedKeys();
Iterator it = readyKeys.iterator();
while (it.hasNext()){
SelectionKey key=null;
try{
key = (SelectionKey) it.next();
it.remove();
if (key.isReadable()) {
receive(key);
}
if (key.isWritable()) {
send(key);
}
}catch(IOException e){
e.printStackTrace();
try{
if(key!=null){
key.cancel();
key.channel().close();
}
}catch(Exception ex){e.printStackTrace();}
}
}//#while
}//#while
}
public void send(SelectionKey key)throws IOException{
ByteBuffer buffer=(ByteBuffer)key.attachment();
SocketChannel socketChannel=(SocketChannel)key.channel();
buffer.flip(); //把极限设为位置
String data=decode(buffer);
if(data.indexOf("\n")==-1)return;
String outputData=data.substring(0,data.indexOf("\n")+1);
System.out.print(outputData);
ByteBuffer outputBuffer=encode("echo:"+outputData);
while(outputBuffer.hasRemaining())
socketChannel.write(outputBuffer);
ByteBuffer temp=encode(outputData);
buffer.position(temp.limit());
buffer.compact();
if(outputData.equals("bye\r\n")){
key.cancel();
socketChannel.close();
System.out.println("关闭与客户的连接");
}
}
public void receive(SelectionKey key)throws IOException{
ByteBuffer buffer=(ByteBuffer)key.attachment();
SocketChannel socketChannel=(SocketChannel)key.channel();
ByteBuffer readBuff= ByteBuffer.allocate(32);
socketChannel.read(readBuff);
readBuff.flip();
buffer.limit(buffer.capacity());
buffer.put(readBuff);
}
public String decode(ByteBuffer buffer){ //解码
CharBuffer charBuffer= charset.decode(buffer);
return charBuffer.toString();
}
public ByteBuffer encode(String str){ //编码
return charset.encode(str);
}
public static void main(String args[])throws Exception{
final EchoServer server = new EchoServer();
Thread accept=new Thread(){
public void run(){
server.accept();
}
};
accept.start();
server.service();
}
}
分享到:
相关推荐
A simple echo Server
关于lwip echo server 例程1
工具版本:Vivado2017.4 开发板:xilinx KC705 实现example:Helloworld,memory test,Lwip echo server 内涵:完整工程
基于epoll的高性能回射服务器.C++封装的agent模式
将并发echo server改造成多线程形式 注意线程竞速问题的解决
simple net framework , echo server use qihoo360 open source code evpp
libevent example. echo server. use c++11 gcc 4.8.5 thread_pool singleton , io thread , servant thread layer
使用recv_peek实现按行读取readline(只能用于socket)
tcp echo server 與 client 可以做 tcp 簡單的溝通
udp echo client server c from unp
基于EchoServer对Netty4.1源码进行分析,水平有限,如果有问题大家可以提出来共同分享。
tcpip echo server source
echo server java版的echo server 一些基礎的概念:)
自定义应用层协议实践 发报文时:前四个字节长度+报文内容一次性发送; 收报文时:先读前四个字节,求出报文内容长度;根据长度读数据。 发送结构:
我们的echo服务器最大的缺点就是无法支持多客户连接,即使客户端能够连接到服务器上,服务器也不为该客户做服务,(直接没什么反应),虽然链接是有的(也就是说,客户端是已经连接到服务器上的了,但是服务器就是不搭理你......
EchoClient EchoServer 客户 服务器程序 通过TCPIP协议完成网络信息传递
这个是简单的echo服务器和对应的客户端,主要是用来测试网络用的
udp echo server and client use them they r working fine
一个简单的EchoServer,可以辅助测试自己的一些网络程序
IOCP echo server client 服务器+客户端 这里可以看主要IOCP 代码http://lovecreat.eu5.org/?p=106