Пример #1
0
 static String printSocket(DatagramSocket socket) {
   return "[Socket: localPort="
       + socket.getLocalPort()
       + ", remoteAddr="
       + socket.getInetAddress()
       + ", localAddr="
       + socket.getLocalAddress()
       + "]";
 }
  public static void main(String[] args) throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    DatagramSocket socket = dc.socket();
    socket.bind(new InetSocketAddress(22500));
    ByteBuffer byteBuf = ByteBuffer.allocate(8);
    //        byteBuf.order(ByteOrder.LITTLE_ENDIAN);
    LongBuffer longBuf = byteBuf.asLongBuffer();

    while (true) {
      dc.receive(byteBuf);
      System.out.println(longBuf.get(0));
      byteBuf.clear();
    }
  }
  public static void main(String args[]) throws Exception {
    final int ENOUGH_SIZE = 1024;
    final int SMALL_SIZE = 4;

    boolean isBlocked = true;
    int size = ENOUGH_SIZE;

    if (args.length > 0) {
      int opt = Integer.parseInt(args[0]);
      switch (opt) {
        case 1:
          isBlocked = true;
          size = ENOUGH_SIZE;
          break;
        case 2:
          isBlocked = true;
          size = SMALL_SIZE;
          break;
        case 3:
          isBlocked = false;
          size = ENOUGH_SIZE;
          break;
        case 4:
          isBlocked = false;
          size = SMALL_SIZE;
          break;
      }
    }

    DatagramChannel channel = DatagramChannel.open();
    channel.configureBlocking(isBlocked);
    ByteBuffer buffer = ByteBuffer.allocate(size);
    DatagramSocket socket = channel.socket();
    SocketAddress localAddr = new InetSocketAddress(8000);
    socket.bind(localAddr);

    while (true) {
      System.out.println("开始接收数据报");
      SocketAddress remoteAddr = channel.receive(buffer);
      if (remoteAddr == null) {
        System.out.println("没有接收到数据报");
      } else {
        buffer.flip();
        System.out.println("接收到的数据报的大小为" + buffer.remaining());
      }
      Thread.sleep(500);
    }
  }