java NiO练习

import io.netty.channel.ChannelFactory;
import org.apache.tools.ant.taskdefs.condition.Socket;
import org.springframework.expression.spel.ast.Selection;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;

public class NioLXService {

	private String ip;
	private int port;
	private static Selector selector;
	private static ServerSocketChannel serverSocketChannel;

	public static void main(String[] args) {
		NioLXService nioLXService = new NioLXService();
		new Thread(()-> {
			try {
				while (true) {
					if(selector.isOpen()){
						int readyChannels = selector.select();
						if(readyChannels==0)continue;
						Set<SelectionKey> selectionKeys = selector.selectedKeys();
						if(selectionKeys != null){
							Iterator<SelectionKey> iterator = selectionKeys.iterator();
							while (iterator.hasNext()){
								SelectionKey selectionKey = iterator.next();

								if(selectionKey.isConnectable()){
									ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
									SocketChannel socketChannel = serverSocketChannel.accept();
									socketChannel.configureBlocking(false);
									socketChannel.register(selector,SelectionKey.OP_ACCEPT);
									System.out.println("连接完成");
								}
								if(selectionKey.isAcceptable()){
									ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
									SocketChannel socketChannel = serverSocketChannel.accept();
									socketChannel.configureBlocking(false);
									socketChannel.register(selector,SelectionKey.OP_READ);
									System.out.println("注册完成");
								}
								if(selectionKey.isReadable()){
									SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
									ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
									int k = socketChannel.read(byteBuffer);
									while (k > 0) {
										byteBuffer.flip();
										byte by[] = new byte[1024];
										while (byteBuffer.hasRemaining()) {
											System.out.print((char) byteBuffer.get());
										}
										byteBuffer.clear();
										k = socketChannel.read(byteBuffer);
									}
									socketChannel.register(selector, SelectionKey.OP_WRITE);
									System.out.println("正在读取");
								}
								if(selectionKey.isWritable()){
									SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
									ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
									String httpResponse = "HTTP/1.1 200 OK\r\n" +
										"Content-Length: 37\r\n" +
										"Content-Type:text/html;charset=utf-8\r\n" +
										"\r\n" +
										"<html><body>你好,世界!</body></html>";
									byteBuffer.put(httpResponse.getBytes());
									byteBuffer.flip();
									while (byteBuffer.hasRemaining()) {
										socketChannel.write(byteBuffer);
									}
									socketChannel.close();
									System.out.println("正在写入");
									selectionKey.cancel();
								}
								iterator.remove();
							}

						}
					}
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}).start();
	}



	public NioLXService(){
		this("localhost",1234);
	}

	public NioLXService(String ip,int port){
		this.ip = ip;
		this.port = port;
		try {
			selector = Selector.open();
			this.serverSocketChannel = ServerSocketChannel.open();
			serverSocketChannel.bind(new InetSocketAddress(ip,port));
			serverSocketChannel.configureBlocking(false);
			serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
			System.out.println("服务端启动成功,IP:"+ip+"端口为:"+port);
		}catch (IOException e){
			e.printStackTrace();
		}
	}
}
java NIO服务端练习,只能输出你好世界。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值