com.mpush.tools.Utils.getLocalNetworkInterface()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(4.8k)|赞(0)|评价(0)|浏览(98)

本文整理了Java中com.mpush.tools.Utils.getLocalNetworkInterface()方法的一些代码示例,展示了Utils.getLocalNetworkInterface()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.getLocalNetworkInterface()方法的具体详情如下:
包路径:com.mpush.tools.Utils
类名称:Utils
方法名:getLocalNetworkInterface

Utils.getLocalNetworkInterface介绍

暂无

代码示例

代码示例来源:origin: mpusher/mpush

@Test
  public void testSend() throws Exception {
    String host = "239.239.239.99";//多播地址
    int port = 9998;
    String message = "test-multicastSocket";
    try {
      InetAddress group = InetAddress.getByName(host);
      MulticastSocket s = new MulticastSocket();
      //加入多播组
      s.joinGroup(new InetSocketAddress(host, port), Utils.getLocalNetworkInterface());
      DatagramPacket dp = new DatagramPacket(message.getBytes(), message.length(), group, port);
      s.send(dp);
      s.close();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }
}

代码示例来源:origin: mpusher/mpush

@Test
public void TestServer() throws Exception {
  //接受组播和发送组播的数据报服务都要把组播地址添加进来
  String host = "239.239.239.88";//多播地址
  int port = 9998;
  InetAddress group = InetAddress.getByName(host);
  DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);
  channel.bind(new InetSocketAddress(port));
  channel.join(group, Utils.getLocalNetworkInterface());
  ByteBuffer buffer = ByteBuffer.allocate(1024);
  SocketAddress sender = channel.receive(buffer);
  buffer.flip();
  byte[] data = new byte[buffer.remaining()];
  buffer.get(data);
  System.out.println(new String(data));
}

代码示例来源:origin: mpusher/mpush

@Test
public void TestServer() throws Exception {
  //接受组播和发送组播的数据报服务都要把组播地址添加进来
  String host = "239.239.239.99";//多播地址
  int port = 9998;
  int length = 1024;
  byte[] buf = new byte[length];
  MulticastSocket ms = null;
  DatagramPacket dp = null;
  StringBuffer sbuf = new StringBuffer();
  try {
    ms = new MulticastSocket(port);
    dp = new DatagramPacket(buf, length);
    //加入多播地址
    ms.joinGroup(new InetSocketAddress(host, port), Utils.getLocalNetworkInterface());
    System.out.println("监听多播端口打开:");
    ms.receive(dp);
    ms.close();
    int i;
    for (i = 0; i < 1024; i++) {
      if (buf[i] == 0) {
        break;
      }
      sbuf.append((char) buf[i]);
    }
    System.out.println("收到多播消息:" + sbuf.toString());
  } catch (IOException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: mpusher/mpush

InetAddress group = InetAddress.getByName(host);
ms.joinGroup(new InetSocketAddress("239.239.239.88", 9999), Utils.getLocalNetworkInterface());
System.out.println("监听多播端口打开:");
DatagramPacket dp2 = new DatagramPacket(message.getBytes(), message.length(), group, port);

代码示例来源:origin: mpusher/mpush

@Test
  public void testSend() throws Exception {
    String host = "239.239.239.99";//多播地址
    int port = 9999;
    InetAddress group = InetAddress.getByName(host);
    String message = "test-multicastSocket";

    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);
    channel.configureBlocking(true);
    channel.bind(new InetSocketAddress(port));
    channel.join(group, Utils.getLocalNetworkInterface());

    InetSocketAddress sender = new InetSocketAddress("239.239.239.99", 4000);
    channel.send(ByteBuffer.wrap(message.getBytes()), sender);

    channel.close();
  }
}

代码示例来源:origin: mpusher/mpush

@Override
public void init() {
  super.init();
  messageDispatcher.register(Command.OK, () -> new GatewayOKHandler(mPushClient));
  messageDispatcher.register(Command.ERROR, () -> new GatewayErrorHandler(mPushClient));
  channelHandler = new UDPChannelHandler(messageDispatcher);
  channelHandler.setMulticastAddress(Utils.getInetAddress(CC.mp.net.gateway_client_multicast));
  channelHandler.setNetworkInterface(Utils.getLocalNetworkInterface());
}

代码示例来源:origin: mpusher/mpush

@Override
public void init() {
  super.init();
  messageDispatcher.register(Command.GATEWAY_PUSH, () -> new GatewayPushHandler(mPushServer.getPushCenter()));
  messageDispatcher.register(Command.GATEWAY_KICK, () -> new GatewayKickUserHandler(mPushServer.getRouterCenter()));
  channelHandler.setMulticastAddress(Utils.getInetAddress(CC.mp.net.gateway_server_multicast));
  channelHandler.setNetworkInterface(Utils.getLocalNetworkInterface());
}

代码示例来源:origin: com.github.mpusher/mpush-client

@Override
public void init() {
  super.init();
  messageDispatcher.register(Command.OK, () -> new GatewayOKHandler(mPushClient));
  messageDispatcher.register(Command.ERROR, () -> new GatewayErrorHandler(mPushClient));
  channelHandler = new UDPChannelHandler(messageDispatcher);
  channelHandler.setMulticastAddress(Utils.getInetAddress(CC.mp.net.gateway_client_multicast));
  channelHandler.setNetworkInterface(Utils.getLocalNetworkInterface());
}

代码示例来源:origin: com.github.mpusher/mpush-core

@Override
public void init() {
  super.init();
  messageDispatcher.register(Command.GATEWAY_PUSH, () -> new GatewayPushHandler(mPushServer.getPushCenter()));
  messageDispatcher.register(Command.GATEWAY_KICK, () -> new GatewayKickUserHandler(mPushServer.getRouterCenter()));
  channelHandler.setMulticastAddress(Utils.getInetAddress(CC.mp.net.gateway_server_multicast));
  channelHandler.setNetworkInterface(Utils.getLocalNetworkInterface());
}

相关文章