NIO示例

NIO服务端代码:

public class NIOServer {

private Selector selector;


public void startServer(int port) throws IOException {

ServerSocketChannel serverChannel = ServerSocketChannel.open();
// 设置通道为非阻塞
serverChannel.configureBlocking(false);

serverChannel.socket().bind(new InetSocketAddress(port));

this.selector = Selector.open();

serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}


public void listen() throws IOException {
while (true) {
//当注册的事件到达时,方法返回;否则,该方法会一直阻塞
selector.select();
Iterator<SelectionKey> it = this.selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
// 此处一定要删除
it.remove();

if (key.isAcceptable()) {

ServerSocketChannel server = (ServerSocketChannel) key
.channel();

SocketChannel channel = server.accept();
System.out.println("remote socket is:"+channel.socket().getInetAddress());
channel.configureBlocking(false);

channel.write(ByteBuffer.wrap(new String("send one msg to client").getBytes()));

channel.register(this.selector, SelectionKey.OP_READ);

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

}

}
}

public void read(SelectionKey key) throws IOException{

SocketChannel channel = (SocketChannel) key.channel();

ByteBuffer buffer = ByteBuffer.allocate(1024);
int len = channel.read(buffer);
byte[] data = new byte[len];
System.arraycopy(buffer.array(), 0, data, 0, len);
String msg = new String(data).trim();
System.out.println("server receive one msg:"+msg);
ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes());
channel.write(outBuffer);
}

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

}


NIO客户端代码:

public class NIOClient {

private Selector selector;


public void initClient(String ip,int port) throws IOException {

SocketChannel channel = SocketChannel.open();

channel.configureBlocking(false);

this.selector = Selector.open();

channel.connect(new InetSocketAddress(ip,port));
channel.register(selector, SelectionKey.OP_CONNECT);
}

public void listen() throws IOException {
while (true) {
selector.select();
Iterator<SelectionKey> it = this.selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();

it.remove();

if (key.isConnectable()) {
SocketChannel channel = (SocketChannel) key.channel();
if(channel.isConnectionPending()){
channel.finishConnect();

}

channel.configureBlocking(false);

channel.write(ByteBuffer.wrap(new String("你在他乡还好吗").getBytes()));

channel.register(this.selector, SelectionKey.OP_READ);

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

}

}
}

public void read(SelectionKey key) throws IOException{
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int len = channel.read(buffer);
byte[] data = new byte[len];
System.arraycopy(buffer.array(), 0, data, 0, len);
String msg = new String(data).trim();
System.out.println("client receive one msg:"+msg);
ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes());
}


public static void main(String[] args) throws IOException {
NIOClient client = new NIOClient();
client.initClient("localhost",8080);
client.listen();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值