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(); } }}
}
}
- nio编程与io编程
- Java网络编程-Nio IO 比较
- Java IO/NIO网络编程资料
- NIO编程
- NIO编程
- NIO编程
- NIO编程
- 二、java网络io编程(BIO、NIO)
- Java 网络IO编程总结(BIO、NIO、AIO)
- Java 网络IO编程总结二(BIO、NIO、AIO)
- 【网络编程】(三)IO模型:BIO、NIO、AIO
- Socket编程(二)-UDP与NIO
- IO编程与线程概念
- io与nio 区别
- Java NIO与IO
- Java NIO与IO
- Java NIO与IO
- Java NIO与IO
- 机器学习之python基础(四)
- arduino学习笔记 一
- Android进阶之使用自定义的字体库
- Catch Bug
- BLE中Profile是什么意思?
- nio编程与io编程
- C语言中变量定义的位置(C89和C99的区别)
- Linux IPC实践--System V消息队列(3)
- org.hibernate.exception.GenericJDBCException: Cannot open connection
- 天声人語 20151106 ベーコンに発がん性?
- linux/mac vi命令详解
- 神的规范:排序算法(二):简单选择排序
- 日经春秋 20151106
- hiho 7 完全背包