zoukankan      html  css  js  c++  java
  • 使用JAVA NIO实现的UDP client和server

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    client.java

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    package udp.nio;

    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.SocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.DatagramChannel;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.util.Arrays;
    import java.util.Iterator;

    import test.PublicTool;

    public class UDPClient {
    static DatagramChannel channel;
    static Selector selector;
    SocketAddress sa;
    static ByteBuffer byteBuffer1;
    static ByteBuffer byteBuffer2 = ByteBuffer.allocate(100);

    public static void run() {
    try {
    channel = DatagramChannel.open();
    channel.configureBlocking(false);
    channel.socket().bind(new InetSocketAddress(10000));
    } catch (Exception e) {
    e.printStackTrace();
    }
    try {
    selector = Selector.open();
    channel.register(selector, SelectionKey.OP_READ);
    byte[] ret = { 0x7C, 0x13, 0x1F, 0x47, 0x0E, 0x03, 0x06, 0x0F,
    0x13, 0x30, 0x00, 0x08, 0x00, 0x00, 0x00, 0x55,
    (byte) 0xAA, 0x70, 0x7A };
    byteBuffer1 = ByteBuffer.wrap(ret);
    channel.send(byteBuffer1, new InetSocketAddress("192.168.1.200",
    10000));
    } catch (Exception e) {
    e.printStackTrace();
    }

    int num = 0;
    while (num < 2) {
    try {
    System.out.println("1111");
    int n = selector.select();
    System.out.println(n);
    if (n > 0) {
    Iterator iterator = selector.selectedKeys().iterator();
    System.out.println(iterator);
    while (iterator.hasNext()) {
    SelectionKey key = (SelectionKey) iterator.next();
    iterator.remove();
    if (key.isReadable()) {
    channel = (DatagramChannel) key.channel();
    channel.receive(byteBuffer2);
    byte[] all = byteBuffer2.array();
    byte[] receiveBuf = new byte[all[1]];
    System.arraycopy(all, 0, receiveBuf, 0, all[1]);
    PublicTool.printHexString(receiveBuf);
    byteBuffer2.clear();
    if(receiveBuf == null || receiveBuf == new byte[0]);break;
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    channel.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

    public static void main(String[] args) {
    while (true) {
    run();
    try {
    Thread.sleep(1000l);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    server

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    package udp.nio;

    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.DatagramChannel;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.util.Arrays;
    import java.util.Iterator;

    import test.PublicTool;

    public class UDPServer
    {
    DatagramChannel channel;

    Selector selector;

    public void work()
    {
    try
    {
    // 打开一个UDP Channel
    channel = DatagramChannel.open();

    // 设定为非阻塞通道
    channel.configureBlocking(false);
    // 绑定端口
    channel.socket().bind(new InetSocketAddress(10000));

    // 打开一个选择器
    selector = Selector.open();
    channel.register(selector, SelectionKey.OP_READ);
    } catch (Exception e)
    {
    e.printStackTrace();
    }

    ByteBuffer byteBuffer = ByteBuffer.allocate(19);
    while (true)
    {
    try
    {
    // 进行选择
    int n = selector.select();
    if (n > 0)
    {
    // 获取以选择的键的集合
    Iterator iterator = selector.selectedKeys().iterator();

    while (iterator.hasNext())
    {
    SelectionKey key = (SelectionKey) iterator.next();

    // 必须手动删除
    iterator.remove();

    if (key.isReadable())
    {
    DatagramChannel datagramChannel = (DatagramChannel) key
    .channel();

    byteBuffer.clear();
    // 读取
    InetSocketAddress address = (InetSocketAddress) datagramChannel
    .receive(byteBuffer);

    PublicTool.printHexString(byteBuffer.array());

    // 删除缓冲区中的数据
    byteBuffer.clear();
    byte[] ret = {0x55, (byte) 0xAA, 0x70, 0x7A };

    byteBuffer.put(ret);

    byteBuffer.flip();

    // 发送数据
    datagramChannel.send(byteBuffer, address);
    }
    }
    }
    } catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    }

    public static void main(String[] args)
    {
    new UDPServer().work();

    }

    }

  • 相关阅读:
    Asp.NET 4.0 ajax实例DataView 模板编程1
    ASP.NET 4.0 Ajax 实例DataView模板编程 DEMO 下载
    部分东北话、北京话
    .NET 培训课程解析(一)
    ASP.NET 4.0 Ajax 实例DataView模板编程2
    ASP.NET Web Game 架构设计1服务器基本结构
    ASP.NET Web Game 构架设计2数据库设计
    TFS2008 基本安装
    Linux上Oracle 11g安装步骤图解
    plsql developer远程连接oracle数据库
  • 原文地址:https://www.cnblogs.com/qingchen1984/p/4159767.html
Copyright © 2011-2022 走看看