利用NIO的Selector处理服务器-客户端模型

内容:这是一个简单的服务器-客户端模型,利用了NIO的Selector来处理多个管道。至于Selector的介绍看这里

NIOServer:

public class NIOServer {
	public static void main(String[] args) throws IOException, InterruptedException {
		Selector selector = Selector.open();

		ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
		InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 8080);
		serverSocketChannel.socket().bind(address);
		serverSocketChannel.configureBlocking(false);
		serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

		while (true) {
			if (selector.select() > 0) {
				Set<SelectionKey> selectionKeys = selector.selectedKeys();
				Iterator<SelectionKey> it = selectionKeys.iterator();
				while (it.hasNext()) {
					SelectionKey selectionKey = it.next();
					if (selectionKey.isAcceptable()) {
						serverSocketChannel = (ServerSocketChannel)selectionKey.channel();
						SocketChannel socketChannel = serverSocketChannel.accept();
						socketChannel.configureBlocking(false);
						socketChannel.register(selector, SelectionKey.OP_READ);
						System.out.println("Connected: " + socketChannel.socket().getRemoteSocketAddress());
					} else if (selectionKey.isReadable()) {
						SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
						ByteBuffer buffer = ByteBuffer.allocate(1024);
						while (socketChannel.read(buffer) > 0) {
							buffer.flip();
							byte[] dis = new byte[buffer.limit()];
							buffer.get(dis);
							System.out.println(new String(dis));
						}
					}

					it.remove();
				}
			}

			Thread.sleep(100);
		}
	}
}

NIOClient:

public class NIOClient {
	public static void main(String[] args) throws IOException {
		SocketChannel socketChannel = SocketChannel.open();
		InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 8080);
		socketChannel.socket().connect(address);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		ByteBuffer buffer = ByteBuffer.allocate(1024);
		while (true) {
				try {
					buffer.clear();
					String time = sdf.format(new Date());
					buffer.put(time.getBytes());
					buffer.flip();
					socketChannel.write(buffer);
					Thread.sleep(5000);
				} catch (Exception e) {
					System.out.println("Connection Close");
					break;
				}
		}
	}
}
时间: 2024-08-24 13:18:32

利用NIO的Selector处理服务器-客户端模型的相关文章

一个简单的JAVA服务器-客户端模型

package com.sxt.udp; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException; public class UdpClient { public static void main(String[] args) throws Exception { System.out.pri

一个简单的JAVA服务器-客户端模型-tcp

package com.sxt.tcp; import java.io.DataInputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { System.out.println("----Server---

java 利用NIO建立Socket服务器

Socket的Channel在Selector上注册某一种动作,Selector通过select操作,监视所有在该Selector注册过的Channel的对应的动作,如果监测到某一对应的动作,则返回selectedKeys,自己手动取到各个SelectionKey进行相应的处理.当然NIO不仅可以接受Socket的Channel,还有文件操作等其他IO操作. AD: WOT2015 互联网运维与开发者大会 热销抢票 传统的Java 的IO,利用Socket建立服务器,接收客户端连接,一般都是为每

java的nio之:java的nio的服务器实现模型

[nio服务端序列图] 一:nio服务器启动类 1 package com.yeepay.sxf.testnio; 2 /** 3 * nio创建的的timerServer服务器 4 * 5 * @author sxf 6 * 7 */ 8 public class NIOTimerServer { 9 10 /** 11 * nio服务器启动的入口 12 * @param args 13 */ 14 public static void main(String[] args) { 15 //启

linux-socket tcp客户端服务器编程模型及代码详解

上一篇文章介绍了 TCP/IP相关协议,socket通信流程和涉及到的各种函数: Socket简单理解 本篇将具体解释tcp客户端服务器编程模型相关的代码 文章分为4个部分: 1. TCP客户端服务器编程模型流程图 2. 网络字节序与主机字节序 3. TCP编程的地址结构 4. 详细案例代码及解释 一: TCP客户端服务器编程模型流程图 上面两张图片将整个流程已经说明的很清楚了; 二: 网络字节序与主机字节序 字节序即是保存数据的方向方式, 分为 大端存储 和 小端存储; 其中 网络字节序 使用

Linux 下 简单客户端服务器通讯模型(TCP)

原文:Linux 下 简单客户端服务器通讯模型(TCP) 服务器端:server.c #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<sys/socket.h> #include<sys/types.h> #include <stdio.h> #include <unistd.h> #inclu

Java 利用 Socket 实现服务器客户端聊天

Socket是网络编程中最基本的通信接口,常用的网络辅助类,比如URL等之类,其底层还是基于Socket来实现的. 而Socket,形象来说,就是连接通信的两端,比如这样 S<==>S,中间的通道就是网络了,而简单地利用Socket,我们就可以来实现一个简单的聊天功能 具体效果看下图: 这只是在本地中试用的效果,如果加上UI界面,其实就可以做成一个聊天的小应用了. 1. Server 端主要是利用ServerSocket的accept方法来等待客户端的连接,如果客户一直没有连接,则会在这里等待

NIO selector 多路复用reactor线程模型

NIO selector 多路复用reactor线程模型 package com.study.hc.net.nio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.util.Iterator; import java.util.Random; import java.util.Set

Java NIO 之 Selector 练习

目的:本编文章主要想分享一下NIO方面的知识,由于最近几天工作不忙,趁机学习了下Java NIO Selector的相关知识:主要是实践操作的:具体的理论知识,可以参考网上的文章. 测试用例主要有三种方式: 其实,是服务器端的逻辑不变,客户端有三种方式而已. 服务器端:2个selector + channel, 客户端:一个channel 服务器端:2个selector + channel, 客户端:多个channel(多线程方式) 服务器端:2个selector + channel, 客户端: