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);        }    }}
原创粉丝点击