1. import java.io.IOException;
    2. import java.net.InetSocketAddress;
    3. import java.nio.ByteBuffer;
    4. import java.nio.channels.DatagramChannel;
    5. public class Demo07 {
    6. //客户端
    7. public static void main(String[] args) throws IOException {
    8. DatagramChannel dc = DatagramChannel.open();
    9. dc.configureBlocking(false);
    10. ByteBuffer buffer = ByteBuffer.allocate(1024);
    11. String msg = "hello udp";
    12. buffer.put(msg.getBytes());
    13. buffer.flip();
    14. dc.send(buffer, new InetSocketAddress("127.0.0.1", 9009));
    15. }
    16. }
    1. import java.net.InetSocketAddress;
    2. import java.nio.ByteBuffer;
    3. import java.nio.channels.DatagramChannel;
    4. import java.nio.channels.SelectionKey;
    5. import java.nio.channels.Selector;
    6. import java.util.Iterator;
    7. import java.util.Set;
    8. public class Demo06 {
    9. //服服务端
    10. public static void main(String[] args) throws Exception {
    11. int port = 9009;
    12. Selector selector = Selector.open();
    13. //和TCP的对象不一样
    14. DatagramChannel dc = DatagramChannel.open();
    15. dc.configureBlocking(false);
    16. dc.bind(new InetSocketAddress(port));
    17. dc.register(selector, SelectionKey.OP_READ);
    18. while (true) {
    19. selector.select();
    20. Set<SelectionKey> keys = selector.selectedKeys();
    21. Iterator<SelectionKey> it = keys.iterator();
    22. while (it.hasNext()) {
    23. SelectionKey key = it.next();
    24. if (key.isReadable()) {
    25. ByteBuffer buffer = ByteBuffer.allocate(1024);
    26. dc.receive(buffer);
    27. buffer.flip();
    28. System.out.println(new String(buffer.array(), 0, buffer.limit()));
    29. }
    30. it.remove();
    31. }
    32. }
    33. }
    34. }

    Pipe通道

    1. import java.io.IOException;
    2. import java.nio.ByteBuffer;
    3. import java.nio.channels.Pipe;
    4. public class Demo08 {
    5. public static void main(String[] args) throws Exception {
    6. //打开通道
    7. Pipe pipe = Pipe.open();
    8. // sink 数据写到这里面
    9. // sourse 通过他读出来
    10. Thread s = new Thread(new Sender(pipe));
    11. Thread r = new Thread(new Recv(pipe));
    12. s.start();
    13. r.start();
    14. }
    15. //写
    16. static class Sender implements Runnable {
    17. Pipe.SinkChannel sinkChannel;
    18. public Sender(Pipe pipe) {
    19. sinkChannel = pipe.sink();
    20. }
    21. @Override
    22. public void run() {
    23. try {
    24. ByteBuffer buf = ByteBuffer.allocate(1024);
    25. buf.put("hello pipe".getBytes());
    26. buf.flip();
    27. sinkChannel.write(buf);
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. }
    31. }
    32. }
    33. //读
    34. static class Recv implements Runnable {
    35. Pipe.SourceChannel sourceChannel;
    36. public Recv(Pipe pipe) {
    37. sourceChannel = pipe.source();
    38. }
    39. @Override
    40. public void run() {
    41. try {
    42. ByteBuffer buff = ByteBuffer.allocate(1024);
    43. int len = sourceChannel.read(buff);
    44. System.out.println(new String(buff.array(), 0, len));
    45. } catch (IOException e) {
    46. e.printStackTrace();
    47. }
    48. }
    49. }
    50. }