java NIO SocketClinet ServerSocket

hgs19921112發表於2018-08-19
//SocketClient.java
//---------------------------------------------------------------------------------
package niotest;
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.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class SocketClient {
public static void main(String[] args) throws IOException {
Selector sc = Selector.open();
SocketChannel skc = SocketChannel.open();
skc.configureBlocking(false);
skc.connect(new InetSocketAddress("127.0.0.1", 44444));
skc.register(sc, SelectionKey.OP_CONNECT);
while(true) {
sc.select();
Set<SelectionKey> selectkey = sc.selectedKeys();
Iterator it = selectkey.iterator();
while(it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
if(key.isConnectable()) {
SocketChannel msk = (SocketChannel) key.channel();
if(!msk.isConnected()) {
while(!msk.finishConnect()) {}
}
msk.register(sc, SelectionKey.OP_WRITE);
}else if (key.isReadable()) { 
}else if(key.isWritable()) {
SocketChannel msk = (SocketChannel) key.channel();
ByteBuffer bb = ByteBuffer.wrap(new String("hellow world!").getBytes());
msk.write(bb);
while (bb.hasRemaining()){
msk.write(bb);
}
key.cancel();
}else if(key.isAcceptable()) {
}else {
throw new RuntimeException("unknow selection type!");
}
it.remove();
}
}
}
}
//SocketServer.java
//----------------------------------------------------------------------------------------------------------
package niotest;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
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;
import java.util.Set;
public class SocketServer {
public static void main(String[] args) throws Exception {
ServerSocketChannel sc = ServerSocketChannel.open();
sc.configureBlocking(false);
sc.bind(new InetSocketAddress(44444));
Selector tor = Selector.open();
sc.register(tor, SelectionKey.OP_ACCEPT);
while(true) {
tor.select();
Set<SelectionKey> selectkey = tor.selectedKeys();
Iterator it = selectkey.iterator();
while(it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
if(key.isAcceptable()) {
ServerSocketChannel msk = (ServerSocketChannel) key.channel();
SocketChannel msc = msk.accept();
msc.configureBlocking(false);
msc.register(tor, SelectionKey.OP_READ);
}else if(key.isConnectable()) {
}else if (key.isWritable()) {
}else if(key.isReadable()) {
SocketChannel msc = (SocketChannel) key.channel();
ByteBuffer bb = ByteBuffer.allocate(17);
while(bb.hasRemaining()) {
msc.read(bb);
}
System.out.println("服務端:"+new String(bb.array()));
}else {
throw  new RuntimeException("unknow selection!");
}
it.remove();
}
}
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31506529/viewspace-2200357/,如需轉載,請註明出處,否則將追究法律責任。