io-3-socket通道

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);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值