您的位置:首页 > 编程语言 > Java开发

java nio demo

2016-07-21 15:56 555 查看
Server

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 Server {
private Selector selector;

public void initServer() throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(8989));

selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

}

public void listen() throws IOException {
System.out.println("server started");
while (true) {
selector.select();
Iterator iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = (SelectionKey) iterator.next();
iterator.remove();
if (selectionKey.isAcceptable()) {
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.write(ByteBuffer.wrap("hello client, from server".getBytes()));
socketChannel.register(selector, SelectionKey.OP_READ);
} else if (selectionKey.isReadable()) {
read(selectionKey);
}
}
}
}

public void read(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(100);
socketChannel.read(byteBuffer);
byteBuffer.flip();
System.out.println("get client message:" + new String(byteBuffer.array()));
socketChannel.write(ByteBuffer.wrap("i got your msg client, from server".getBytes()));
}

public static void main(String[] args) throws IOException {
Server server = new Server();
server.initServer();
server.listen();
}
}


Client

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;

public class Client {

private Selector selector;

public void initClient() throws IOException {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("localhost", 8989));

selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}

public void listen() throws IOException {
while (true) {
selector.select();
Iterator iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = (SelectionKey) iterator.next();
iterator.remove();
if (selectionKey.isConnectable()) {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
if (socketChannel.isConnectionPending()) {
socketChannel.finishConnect();
}

socketChannel.configureBlocking(false);
socketChannel.write(ByteBuffer.wrap("hello server, i want to connect".getBytes()));
socketChannel.register(selector, SelectionKey.OP_READ);

} else if (selectionKey.isReadable()) {
read(selectionKey);
}
}

}
}

public void read(SelectionKey selectionKey) throws IOException {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(100);
socketChannel.read(byteBuffer);
byteBuffer.flip();
System.out.println("get msg from server:" + new String(byteBuffer.array()));
}

public static void main(String[] args) throws IOException {
Client client = new Client();
client.initClient();
client.listen();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java nio