Java NIO 同步非阻塞Socket DEMO
来源:互联网 发布:网络用语古是什么意思 编辑:程序博客网 时间:2024/05/16 12:10
在这个例子上做的修改:http://blog.csdn.net/eclipser1987/article/details/7329362
Server.java
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.nio.channels.spi.SelectorProvider;import java.util.Iterator;/** * Created by yangankang on 16/2/20. */public class Server implements Runnable { private int port = 8888; private ServerSocketChannel socketChannel; private SocketChannel clientChannel; private Selector selector; private ByteBuffer byteBuffer = ByteBuffer.allocate(512); public Server() { init(); } public void init() { try { this.selector = SelectorProvider.provider().openSelector(); this.socketChannel = ServerSocketChannel.open(); this.socketChannel.configureBlocking(false); this.socketChannel.socket().bind(new InetSocketAddress("localhost", port)); this.socketChannel.register(this.selector, SelectionKey.OP_ACCEPT); } catch (IOException e) { e.printStackTrace(); } } public static void main(String args[]) { Server server = new Server(); Thread thread = new Thread(server); thread.start(); } @Override public void run() { while (true) { try { System.err.println("Server Start."); this.selector.select(); Iterator iterator = this.selector.selectedKeys().iterator(); while (iterator.hasNext()) { SelectionKey key = (SelectionKey) iterator.next(); iterator.remove(); if (!key.isValid()) { continue; } if (key.isAcceptable()) { this.accept(key); } else if (key.isReadable()) { this.read(key); } } } catch (IOException e) { e.printStackTrace(); } } } public void accept(SelectionKey key) { ServerSocketChannel socketChannel = (ServerSocketChannel) key.channel(); try { clientChannel = socketChannel.accept(); clientChannel.configureBlocking(false); clientChannel.register(this.selector, SelectionKey.OP_READ); } catch (IOException e) { e.printStackTrace(); } } public void read(SelectionKey key) { this.byteBuffer.clear(); SocketChannel socketChannel = (SocketChannel) key.channel(); try { int count = socketChannel.read(this.byteBuffer); if (count == -1) { key.channel().close(); key.channel(); return; } String string = new String(this.byteBuffer.array()).trim(); System.out.println("from client message:" + string); socketChannel.write(ByteBuffer.wrap(("you say:" + string).getBytes())); } catch (IOException e) { e.printStackTrace(); } }}
Client.java
import java.io.IOException;import java.net.InetAddress;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;/** * Created by yangankang on 16/2/21. */public class Client { private ByteBuffer buffer = ByteBuffer.allocate(512); private String url = "localhost"; private int port = 8888; public void send() { InetSocketAddress address = new InetSocketAddress(url, port); SocketChannel socketChannel = null; while (true) { byte[] b = new byte[512]; try { System.err.print("input your message:"); System.in.read(b); socketChannel = SocketChannel.open(); socketChannel.connect(address); buffer.clear(); buffer.put(b); buffer.flip(); socketChannel.write(buffer); buffer.clear(); socketChannel.configureBlocking(true); ByteBuffer readBuffer = ByteBuffer.allocate(512); socketChannel.read(readBuffer); System.out.println("from client message:" + (new String(readBuffer.array()).trim())); socketChannel.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (socketChannel != null) try { socketChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String args[]) { Client client = new Client(); client.send(); }}
0 0
- Java NIO 同步非阻塞Socket DEMO
- java NIO实现同步非阻塞服务器
- Java nio Socket非阻塞模式
- Java NIO 非阻塞Socket服务器构建
- java NIO 实现非阻塞socket通信
- Java NIO 非阻塞socket通信案例
- java 同步阻塞的BIO、同步非阻塞的NIO、异步非阻塞的AIO
- socket nio非阻塞通信
- java nio 非阻塞
- java非阻塞nio
- 【Java高级】Java中NIO非阻塞socket通信
- [转]彻底理解java aio,bio.nio 同步 异步 阻塞 非阻塞 同步阻塞 同步非阻塞 异步阻塞 异步非阻塞
- java NIO非阻塞方式的Socket编程
- java NIO非阻塞方式的Socket编程
- java nio实现非阻塞Socket通信实例
- Thinking in Java--使用NIO实现非阻塞Socket通信
- JAVA 网络编程(2) SOCKET 非阻塞NIO示例
- java NIO非阻塞方式的Socket编程
- 再次来到上海,希望这次能坚持下去
- 斐波那契数列
- 如何让ubuntu开机进入纯命令模式
- Linux C判断两个IPv6地址是否相等的方法
- 手机版同步html几点注意使用
- Java NIO 同步非阻塞Socket DEMO
- 面向对象三大特性
- 在Ubuntu上安装Wine
- CodeForces 630 E. A rectangle(水~)
- 【Linux 基础篇】之常用命令
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(离散化+线段树)
- C++ Primer阅读心得(第十六章)
- HDOJ 5630Rikka with Chess(游戏)
- [LeetCode] Same Tree