nio编程与io编程

来源:互联网 发布:宁波seo入门教程 编辑:程序博客网 时间:2024/05/24 06:17

这是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.util.Iterator;

public class NioServer {

Selector selector=null;public void  init() throws IOException{    selector=Selector.open();    ServerSocketChannel sn=ServerSocketChannel.open();    sn.configureBlocking(false);    sn.socket().bind(new InetSocketAddress(8089));    sn.register(selector, SelectionKey.OP_ACCEPT);}public void listen() throws IOException{    System.out.println("****The Server Has Started!****");    while(true){        int count=0;        selector.select();        Iterator it=selector.selectedKeys().iterator();        while(it.hasNext()){        SelectionKey key=(SelectionKey)it.next();        it.remove();        if(key.isAcceptable()){            count++;             ServerSocketChannel sn=(ServerSocketChannel) key.channel();        SocketChannel sc=sn.accept();        sc.configureBlocking(false);       SelectionKey k=sc.register(selector,SelectionKey.OP_READ);        k.attach("wo shi di "+count+" ge  client");        }        if(key.isReadable()){            ByteBuffer bf=ByteBuffer.allocate(100);            SocketChannel sc=(SocketChannel) key.channel();            int len=sc.read(bf);            if(len>0){                bf.flip();            byte[] data=bf.array();            String msg=new String(data,"utf-8");                System.out.println(key.attachment()+" send "+msg);                String back="i  am your Master";                sc.write(bf.wrap(back.getBytes()));            }        }        }    }}public static void main(String[] args){NioServer server=new NioServer();try {    server.init();    server.listen();} catch (IOException e) {    // TODO Auto-generated catch block    e.printStackTrace();}}

}
***************io ***********
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
public static void main(String[] args){
new Server().react();
}
public void react(){
try {
ServerSocket socket=new ServerSocket(8089);
while(true){
Socket con=socket.accept();
new NetThread(con).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class NetThread extends Thread{

Socket s=null;public NetThread(Socket s){    this.s=s;}public void run(){    try {        InputStream in=s.getInputStream();        OutputStream out=s.getOutputStream();        byte[] b=new byte[100];        in.read(b);        System.out.println(new String(b,"utf-8"));    String back="i am your Master";        out.write(back.getBytes());    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }finally{    try {        s.close();    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    }}   

}
}

0 0
原创粉丝点击