nio编程与io编程

这是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();
    }
    }
}   

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值