早就聽說JAVA的NIO比IO牛一些,可是牛在哪里一直都不知道,并且NIO比IO難學,搞了半天終于用NIO弄了兩個程序,一個是服務器端,一個是客戶端,都是用NIO連接的,代碼如下,注釋比較少,輸出比較多:)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testnio;
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.Set;
/**
*
* @author hadeslee
*/
public class Receive {
public static void main(String[] args) throws Exception {
boolean b = true;
ByteBuffer buffer = ByteBuffer.allocate(1024);
ServerSocketChannel ss = ServerSocketChannel.open();
ss.socket().bind(new InetSocketAddress(8888));
ss.configureBlocking(false);
Selector se = Selector.open();
ss.register(se, SelectionKey.OP_ACCEPT);
while (se.select() > 0) {
Set<SelectionKey> set = se.selectedKeys();
System.out.println("進入一個循環,大小是:" + set.size());
for (SelectionKey key : set) {
int ops = key.readyOps();
System.out.println("ops=" + ops);
if ((ops & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
SocketChannel sc = ss.accept();
System.err.println("有新的連接了
" + sc);
System.err.println("地址是:" + sc.socket());
sc.configureBlocking(false);
sc.register(se, SelectionKey.OP_READ);
}
if ((ops & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
System.err.println("有新的讀取");
SocketChannel sc = (SocketChannel) key.channel();
System.out.println(sc.isConnected());
sc.read(buffer);
buffer.flip();
//System.out.println(new String(buffer.array()));
Thread.sleep(5000);
if (b) {
b = false;
sc.write(buffer);
}
}
}
set.clear();
System.out.println("退出循環");
}
}
}
客戶端:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testnio;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Set;
/**
*
* @author hadeslee
*/
public class Send {
public static void main(String[] args) throws Exception {
SocketChannel sc = SocketChannel.open();
ByteBuffer buffer = ByteBuffer.allocate(1024);
Selector se = Selector.open();
buffer.put("我是中國人,我愛我的祖國,hadeslee".getBytes());
buffer.flip();
sc.configureBlocking(false);
sc.register(se, SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);
sc.connect(new InetSocketAddress("192.168.1.58", 8888));
while(!sc.finishConnect());
sc.write(buffer);
System.out.println("進入循環");
Thread.sleep(10000);
int sum = se.select();
while (se.select() > 0) {
Thread.sleep(100);
System.out.println("終于大于0了");
Set<SelectionKey> set = se.selectedKeys();
System.out.println("大小是:"+set.size());
for (SelectionKey key : set) {
int ops = key.readyOps();
if ((ops & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT) {
sc.write(buffer);
System.out.println("連接成功");
}
if ((ops & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
System.out.println(" 收到東西");
sc.read(buffer);
buffer.flip();
System.out.println("收到的是:" + new String(buffer.array(),0,buffer.limit()));
sc.write(buffer);
}
}
se.selectedKeys().clear();
}
}
private static ByteBuffer[] get(String
heads) {
ByteBuffer[] bbs = new ByteBuffer[heads.length];
for (int i = 0; i < bbs.length; i++) {
String s = heads[i];
bbs[i] = ByteBuffer.allocateDirect(1024);
bbs[i].put(s.getBytes());
bbs[i].flip();
}
return bbs;
}
}
有機會再好好研究它們之間的更加具體的用法,以上的只是一個簡單的,能互連的一個例子.
盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.
posted on 2007-11-05 10:50
千里冰封 閱讀(16759)
評論(8) 編輯 收藏 所屬分類:
JAVASE