其他分享
首页 > 其他分享> > NIO之DatagramChannel

NIO之DatagramChannel

作者:互联网

import org.junit.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

public class DatagramChannelDemo {
  //发送的实现
  @Test
  public void sendDatagram() throws IOException, InterruptedException {
    //打开DatagramChannel
    DatagramChannel sendChannel = DatagramChannel.open();
    InetSocketAddress sendAddress = new InetSocketAddress("127.0.0.1", 9999);
    //发送
    while (true) {
      ByteBuffer buffer = ByteBuffer.wrap("发送auguigu".getBytes(StandardCharsets.UTF_8));
      sendChannel.send(buffer, sendAddress);
      System.out.println("已经完成发送," + LocalDateTime.now());
      Thread.sleep(1000);
    }
  }

  //接收的实现
  @Test
  public void receiveDatagram() throws IOException {
    //打开DatagramChannel
    DatagramChannel receiveChannel = DatagramChannel.open();
    InetSocketAddress receiveAddress = new InetSocketAddress("127.0.0.1", 9999);
    //绑定
    receiveChannel.bind(receiveAddress);
    //buffers
    ByteBuffer receiveBuffer = ByteBuffer.allocate(1024);
    while (true) {
      receiveBuffer.clear();
      SocketAddress socketAddress = receiveChannel.receive(receiveBuffer);
      receiveBuffer.flip();
      System.out.println(socketAddress.toString());
      System.out.println(StandardCharsets.UTF_8.decode(receiveBuffer));
    }

  }

  @Test
  public void writeContent() throws IOException, InterruptedException {
    //打开DatagramChannel
    DatagramChannel contentChannel = DatagramChannel.open();
    InetSocketAddress contentAddress = new InetSocketAddress("127.0.0.1", 9999);
    //连接
    contentChannel.connect(contentAddress);
    while (true) {
      ByteBuffer buffer = ByteBuffer.wrap(("发送auguigu" + LocalDateTime.now()).getBytes(StandardCharsets.UTF_8));
      contentChannel.write(buffer);
      System.out.println("已经完成发送," + LocalDateTime.now());
      Thread.sleep(1000);
    }

  }

  @Test
  public void readConect() throws IOException {
    DatagramChannel connChannel = DatagramChannel.open();
    connChannel.bind(new InetSocketAddress(9998));
    connChannel.connect(new InetSocketAddress("127.0.0.1", 9999));
    ByteBuffer readBuffer = ByteBuffer.allocate(512);
    while (true) {
      try {
        readBuffer.clear();
        connChannel.read(readBuffer);
        readBuffer.flip();
        System.out.println(StandardCharsets.UTF_8.decode(readBuffer));
      } catch (Exception e) {

      }
    }
  }

  @Test
  public void writeConect() throws IOException, InterruptedException {
    DatagramChannel connChannel = DatagramChannel.open();
    connChannel.bind(new InetSocketAddress(9999));
    connChannel.connect(new InetSocketAddress("127.0.0.1", 9998));
    while (true) {
      connChannel.write(ByteBuffer.wrap(("发包," + LocalDateTime.now()).getBytes(StandardCharsets.UTF_8)));
      Thread.sleep(1000);
      System.out.println("发包成功" + LocalDateTime.now());
    }
  }

}

标签:java,NIO,DatagramChannel,ByteBuffer,connChannel,import,InetSocketAddress
来源: https://www.cnblogs.com/xl4ng/p/15915188.html