@Override
 public default void setOptions(AsynchronousSocketChannel channel) {
   try {
     channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
     channel.setOption(StandardSocketOptions.SO_RCVBUF, setRCV());
     channel.setOption(StandardSocketOptions.SO_SNDBUF, setSNF());
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
示例#2
0
 @Override
 public Result attempt(
     final List<AvailObject> args, final Interpreter interpreter, final boolean skipReturnCheck) {
   assert args.size() == 2;
   final AvailObject handle = args.get(0);
   final AvailObject options = args.get(1);
   final AvailObject pojo = handle.getAtomProperty(AtomDescriptor.socketKey());
   if (pojo.equalsNil()) {
     return interpreter.primitiveFailure(
         handle.isAtomSpecial() ? E_SPECIAL_ATOM : E_INVALID_HANDLE);
   }
   final AsynchronousSocketChannel socket = (AsynchronousSocketChannel) pojo.javaObjectNotNull();
   try {
     for (final MapDescriptor.Entry entry : options.mapIterable()) {
       final AvailObject key = entry.key();
       final AvailObject entryValue = entry.value();
       assert key != null;
       @SuppressWarnings("rawtypes")
       final SocketOption option = socketOptions[key.extractInt()];
       final Class<?> type = option.type();
       if (type.equals(Boolean.class) && entryValue.isBoolean()) {
         final SocketOption<Boolean> booleanOption = option;
         socket.setOption(booleanOption, entryValue.extractBoolean());
       } else if (type.equals(Integer.class) && entryValue.isInt()) {
         final Integer value = entryValue.extractInt();
         socket.<Integer>setOption(option, value);
       } else {
         return interpreter.primitiveFailure(E_INCORRECT_ARGUMENT_TYPE);
       }
     }
     return interpreter.primitiveSuccess(NilDescriptor.nil());
   } catch (final IllegalArgumentException e) {
     return interpreter.primitiveFailure(E_INCORRECT_ARGUMENT_TYPE);
   } catch (final IOException e) {
     return interpreter.primitiveFailure(E_IO_ERROR);
   }
 }
示例#3
0
  public static void main(String[] args) {

    final int DEFAULT_PORT = 5555;
    final String IP = "127.0.0.1";

    // create asynchronous socket channel bound to the default group
    try (AsynchronousSocketChannel asynchronousSocketChannel = AsynchronousSocketChannel.open()) {

      if (asynchronousSocketChannel.isOpen()) {

        // set some options
        asynchronousSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 128 * 1024);
        asynchronousSocketChannel.setOption(StandardSocketOptions.SO_SNDBUF, 128 * 1024);
        asynchronousSocketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);

        // connect this channel's socket
        asynchronousSocketChannel.connect(
            new InetSocketAddress(IP, DEFAULT_PORT),
            null,
            new CompletionHandler<Void, Void>() {

              final ByteBuffer helloBuffer = ByteBuffer.wrap("Hello !".getBytes());
              final ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
              CharBuffer charBuffer = null;
              ByteBuffer randomBuffer;
              final Charset charset = Charset.defaultCharset();
              final CharsetDecoder decoder = charset.newDecoder();

              @Override
              public void completed(Void result, Void attachment) {
                try {
                  System.out.println(
                      "Successfully connected at: " + asynchronousSocketChannel.getRemoteAddress());

                  // transmitting data
                  asynchronousSocketChannel.write(helloBuffer).get();

                  while (asynchronousSocketChannel.read(buffer).get() != -1) {

                    buffer.flip();

                    charBuffer = decoder.decode(buffer);
                    System.out.println(charBuffer.toString());

                    if (buffer.hasRemaining()) {
                      buffer.compact();
                    } else {
                      buffer.clear();
                    }

                    int r = new Random().nextInt(100);
                    if (r == 50) {
                      System.out.println(
                          "50 was generated! Close the asynchronous socket channel!");
                      break;
                    } else {
                      randomBuffer =
                          ByteBuffer.wrap("Random number:".concat(String.valueOf(r)).getBytes());
                      asynchronousSocketChannel.write(randomBuffer).get();
                    }
                  }
                } catch (IOException | InterruptedException | ExecutionException ex) {
                  System.err.println(ex);
                } finally {
                  try {
                    asynchronousSocketChannel.close();
                  } catch (IOException ex) {
                    System.err.println(ex);
                  }
                }
              }

              @Override
              public void failed(Throwable exc, Void attachment) {
                throw new UnsupportedOperationException("Connection cannot be established!");
              }
            });

        System.in.read();

      } else {
        System.out.println("The asynchronous socket channel cannot be opened!");
      }

    } catch (IOException ex) {
      System.err.println(ex);
    }
  }