2021-04-18 11:31  阅读(81)
文章分类:Java 基础教程 文章标签:JavaJava 教程
©  原文作者:w3cschool 原文地址:https://www.w3cschool.cn/java/java-network-tcp-server.html

Java网络教程 - Java数据报套接字通道

java.nio.channels.DatagramChannel类表示数据报通道。默认情况下,它是阻塞。要使其无阻塞,请使用configureBlocking(false)方法。

要创建DatagramChannel,请调用其open()静态方法之一。

要将其用于IP多播,请将多播组的地址类型指定为其open()方法的参数。

open()方法创建一个没有连接的DatagramChannel对象。

例子

以下代码显示如何基于数据报通道创建Echo服务器。

    import java.net.InetSocketAddress;
    import java.net.SocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.DatagramChannel;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        DatagramChannel server = null;
        server = DatagramChannel.open();
        InetSocketAddress sAddr = new InetSocketAddress("localhost", 8989);
        server.bind(sAddr);
        ByteBuffer buffer = ByteBuffer.allocate(1024);
    
        while (true) {
          System.out.println("Waiting for a  message  from"
              + "  a  remote  host at " + sAddr);
          SocketAddress remoteAddr = server.receive(buffer);
          buffer.flip();
          int limits = buffer.limit();
          byte bytes[] = new byte[limits];
          buffer.get(bytes, 0, limits);
          String msg = new String(bytes);
    
          System.out.println("Client at " + remoteAddr + "  says: " + msg);
          buffer.rewind();
          server.send(buffer, remoteAddr);
          buffer.clear();
        }
        //server.close();
      }
    }
    

上面的代码生成以下结果。

202104181134_1.png

例2

以下代码基于数据报通道创建客户端程序。

    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.DatagramChannel;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        DatagramChannel client = null;
        client = DatagramChannel.open();
    
        client.bind(null);
    
        String msg = "Hello";
        ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
        InetSocketAddress serverAddress = new InetSocketAddress("localhost",
            8989);
    
        client.send(buffer, serverAddress);
        buffer.clear();
        client.receive(buffer);
        buffer.flip();
        int limits = buffer.limit();
        byte bytes[] = new byte[limits];
        buffer.get(bytes, 0, limits);
        String response = new String(bytes);
        System.out.println("Server  responded: " + response);
        client.close();
      }
    }
    

上面的代码生成以下结果。

202104181133_2.png

列出机器上的可用网络接口

    import java.net.NetworkInterface;
    import java.util.Enumeration;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        Enumeration<NetworkInterface> e = NetworkInterface
            .getNetworkInterfaces();
        while (e.hasMoreElements()) {
          NetworkInterface nif = e.nextElement();
          System.out.println("Name: " + nif.getName()
              + ",  Supports Multicast: " + nif.supportsMulticast()
              + ", isUp(): " + nif.isUp());
        }
      }
    }
    

上面的代码生成以下结果。202104181131_3.png

例3

以下代码基于DatagramChannel的组播客户端程序

    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.net.NetworkInterface;
    import java.net.StandardProtocolFamily;
    import java.net.StandardSocketOptions;
    import java.nio.ByteBuffer;
    import java.nio.channels.DatagramChannel;
    import java.nio.channels.MembershipKey;
    
    public class Main {
      public static final String MULTICAST_IP = "239.1.1.1";
      public static final int MULTICAST_PORT = 8989;
    
      public static final String MULTICAST_INTERFACE_NAME = "eth1";
    
      public static void main(String[] args) throws Exception {
        MembershipKey key = null;
        DatagramChannel client = DatagramChannel.open(StandardProtocolFamily.INET);
    
        NetworkInterface interf = NetworkInterface.getByName(MULTICAST_INTERFACE_NAME);
        client.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        client.bind(new InetSocketAddress(MULTICAST_PORT));
        client.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    
        InetAddress group = InetAddress.getByName(MULTICAST_IP);
        key = client.join(group, interf);
    
        System.out.println("Joined the   multicast  group:" + key);
        System.out.println("Waiting for a  message  from  the"
            + "  multicast group....");
    
        ByteBuffer buffer = ByteBuffer.allocate(1048);
        client.receive(buffer);
        buffer.flip();
        int limits = buffer.limit();
        byte bytes[] = new byte[limits];
        buffer.get(bytes, 0, limits);
        String msg = new String(bytes);
    
        System.out.format("Multicast Message:%s%n", msg);
        key.drop();
      }
    }
    

上面的代码生成以下结果。

202104181131_4.png

例4

以下代码显示如何创建向多播组发送消息的基于DatagramChannel的组播程序。

    import java.net.InetSocketAddress;
    import java.net.NetworkInterface;
    import java.net.StandardSocketOptions;
    import java.nio.ByteBuffer;
    import java.nio.channels.DatagramChannel;
    
    public class Main {
      public static final String MULTICAST_IP = "239.1.1.1";
      public static final int MULTICAST_PORT = 8989;
      public static final String MULTICAST_INTERFACE_NAME = "eth1";
    
      public static void main(String[] args) throws Exception {
        DatagramChannel server = DatagramChannel.open();
        server.bind(null);
        NetworkInterface interf = NetworkInterface
            .getByName(MULTICAST_INTERFACE_NAME);
        server.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    
        String msg = "Hello!";
        ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
        InetSocketAddress group = new InetSocketAddress(MULTICAST_IP,
            MULTICAST_PORT);
    
        server.send(buffer, group);
        System.out.println("Sent the   multicast  message: " + msg);
      }
    }
    

上面的代码生成以下结果。

202104181131_5.png

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> Java 数据报套接字通道
上一篇
Java 异步套接字通道