EchoClient:
import java.net.*;
import java.nio.channels.*;
import java.nio.*;
import java.io.*;
import java.nio.charset.*;
import java.util.*;
public class EchoClient86
{
private DatagramChannel datagramChannel = null;
private ByteBuffer sendBuffer=ByteBuffer.allocate(1024);
private ByteBuffer receiveBuffer=ByteBuffer.allocate(1024);
private Charset charset=Charset.forName("GBK");
private Selector selector;
public EchoClient86()throws IOException{
this(7000);
}
public EchoClient86(int port)throws IOException{
datagramChannel = DatagramChannel.open();
InetAddress ia = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(ia,port);
datagramChannel.configureBlocking(false); //设置为非阻塞模式
datagramChannel.socket().bind(isa); //与本地地址绑定
isa = new InetSocketAddress(ia,9999);
datagramChannel.connect(isa); //与远程地址连接
selector=Selector.open();
}
public static void main(String args[])throws IOException
{
int port=7000;
if(args.length>0)port=Integer.parseInt(args[0]);
final EchoClient86 client=new EchoClient86(port);
client.talk();
}
public void talk()throws IOException {
datagramChannel.register(selector,
SelectionKey.OP_READ
);
while (selector.select() > 0 ){
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);
}
}catch(IOException e){
e.printStackTrace();
try{
if(key!=null){
key.cancel();
key.channel().close();
}
}catch(Exception ex){e.printStackTrace();}
}
}//#while
}//#while
}
public void receive(SelectionKey key)throws IOException{
DatagramChannel datagramChannel=(DatagramChannel)key.channel();
datagramChannel.read(receiveBuffer);
receiveBuffer.flip();
String receiveData=decode(receiveBuffer);
if(receiveData.indexOf("\n")==-1)
return;
String outputData=receiveData.substring(0,receiveData.indexOf("\n")+1);
System.out.print(outputData);
if(outputData.equals("no data!\r\n")){
key.cancel();
datagramChannel.close();
System.out.println("关闭与服务器的连接");
selector.close();
System.exit(0);
}
ByteBuffer temp=encode(outputData);
receiveBuffer.position(temp.limit());
receiveBuffer.compact();
}
public String decode(ByteBuffer buffer){ //解码
CharBuffer charBuffer= charset.decode(buffer);
return charBuffer.toString();
}
public ByteBuffer encode(String str){ //编码
return charset.encode(str);
}
}
EchoServer:
import java.io.*;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Handler;
public class EchoServer9 {
private static int port = 9999; //端口号
private ExecutorService executorService; //线程池
private final int POOL_SIZE = 4; //单个CPU时线程池中工作线程的数量
private BufferedReader br = null; //用于存储数据
private static int num = 0; //记录数据
private DatagramSocket datagramSocket; //新建一个datagramSocket
public EchoServer9() throws Exception {
datagramSocket = new DatagramSocket(port);//建立datagramSocket并绑定端口
executorService =
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * POOL_SIZE);
//创建线程池;Runtime的availableProcessors()方法返回当前系统的CPU的数目
br = new BufferedReader(new FileReader("fds_data(lab4).txt")); //BufferedReader存储发送文件的内容
sum();
System.out.println("服务器启动");
}
publi