io-3-socket通道
来源:互联网 发布:hl202控制软件下载 编辑:程序博客网 时间:2024/03/29 21:50
ServerSocketChannel
package com.ygy.test.net.socket;import java.io.IOException;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.nio.ByteBuffer;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;/** * Created by guoyao on 2017/7/17. */public class TestServerChannel { public static void main(String[] args) throws IOException { ByteBuffer buffer=ByteBuffer.wrap(" this is test server socket channel ".getBytes()); ServerSocketChannel serverChannel=ServerSocketChannel.open(); //绑定ServerSocket ServerSocket server=serverChannel.socket(); //只有ServerSocket 可以绑定端口 server.bind(new InetSocketAddress(8888)); //开启非阻塞模式 serverChannel.configureBlocking(false); while (true) { //获取连接 SocketChannel socketChannel = serverChannel.accept(); //非阻塞模式如果没有连接 则为null if (socketChannel == null) { continue; } // position 设置为0 读取数据 buffer.rewind(); socketChannel.write(buffer); socketChannel.close(); } }}
SocketChannel
package com.ygy.test.net.socket;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.Channels;import java.nio.channels.SocketChannel;import java.nio.channels.WritableByteChannel;/** * Created by guoyao on 2017/7/17. */public class TestSocketChannel { public static void main(String[] args) throws IOException { SocketChannel socketChannel=SocketChannel.open(new InetSocketAddress("127.0.0.1", 8888)); //开启非阻塞模式 socketChannel.configureBlocking(false); while (!socketChannel.finishConnect()) { System.out.println(" is conning "); } if (socketChannel.isConnected()) { System.out.println( " is conneted "); } ByteBuffer buffer=ByteBuffer.allocate(2); WritableByteChannel writableByteChannel=Channels.newChannel(System.out); while (socketChannel.read(buffer) > 0) { buffer.flip(); writableByteChannel.write(buffer); buffer.clear(); } socketChannel.close(); writableByteChannel.close(); }}
DatagramChannel
package com.socket.socket;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.channels.DatagramChannel;/** * Created by guoyao on 2017/7/17. */public class TestDatagramClient { public static void main(String[] args) throws Exception { DatagramChannel datagramChannel=DatagramChannel.open(); datagramChannel.configureBlocking(false); datagramChannel.connect(new InetSocketAddress("127.0.0.1", 9999)); ByteBuffer byteBuffer=ByteBuffer.allocate(10); byteBuffer.putLong(0, 2L); datagramChannel.send(byteBuffer,new InetSocketAddress("127.0.0.1", 9999)); ByteBuffer buffer=ByteBuffer.allocate(10); buffer.order(ByteOrder.BIG_ENDIAN); datagramChannel.receive(buffer); buffer.flip(); while (buffer.hasRemaining()) { System.out.println(buffer.getLong(0)); buffer.position(8); } buffer.clear(); } protected static InetSocketAddress receivePacket(DatagramChannel channel, ByteBuffer buffer) throws Exception { buffer.clear(); return ((InetSocketAddress) channel.receive(buffer)); }}
package com.socket.socket;import java.io.IOException;import java.net.DatagramSocket;import java.net.InetSocketAddress;import java.net.SocketAddress;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.channels.DatagramChannel;/** * Created by guoyao on 2017/7/17. */public class TestDatagramChannelServer { public static void main(String[] args) throws IOException { DatagramChannel datagramChannel=DatagramChannel.open(); DatagramSocket socket=datagramChannel.socket(); socket.bind(new InetSocketAddress(9999)); ByteBuffer buffer=ByteBuffer.allocate(8); buffer.order(ByteOrder.BIG_ENDIAN); buffer.putLong(0, 0); buffer.position(4); ByteBuffer byteBuffer=buffer.slice(); while (true) { buffer.clear(); SocketAddress address=datagramChannel.receive(byteBuffer); if (address == null) { continue; } buffer.clear(); buffer.putLong(0,(System.currentTimeMillis()/1000) +2208988800L ) ; datagramChannel.send(buffer, address); } }}
阅读全文
0 0
- io-3-socket通道
- socket通道
- Socket通道
- Socket通道
- NIO channel Socket通道
- JAVA NIO Socket通道
- NIO - Socket 通道
- socket通道小例子
- ioctl ---IO通道管理函数
- ioctl ---IO通道管理函数
- IO 文件输入输出的通道
- socket IO
- SOCKET.IO
- socket io
- IO::Socket
- socket.io
- Socket.io
- socket.io
- java web 注销登录
- 一个小例子认识工厂模式
- thinkphp5 源码分析一 入口文件
- Java开发记录1---JRE环境和Eclipse安装
- python对xml文件写入操作
- io-3-socket通道
- 动态获取文件路径或图片路径
- Hash算法冲突解决方法分析
- github上关于iOS的各种开源项目集合 (转载)
- servlet容器
- liunx 简单操作(持续更新)
- UVa514
- values-v24 Error for android studio
- PAT甲级 1030