Examples of SocketChannel


Examples of java.nio.channels.SocketChannel

        public void handle(SelectionKey key) throws ClosedChannelException, IOException {
            if(!key.isValid()) {
                return;
            }
            SocketChannel channel = (SocketChannel) key.channel();
            if(key.isReadable()) {
                if(doRead(channel, cmdBuffer)) {
                    key.interestOps(SelectionKey.OP_WRITE);
                } else {
                    key.selector().wakeup();
View Full Code Here

Examples of java.nio.channels.SocketChannel

            return createSocketChannel(sockAddr, blocking, soRcvBufSize);
        }
    }

    private static SocketChannel createSocketChannel(final SocketAddress sockAddr, final boolean blocking, final int rcvbufSize) {
        final SocketChannel ch;
        try {
            ch = SocketChannel.open();
            ch.configureBlocking(blocking);
        } catch (IOException e) {
            LOG.error("Failed to open SocketChannel.", e);
            throw new IllegalStateException(e);
        }
        final Socket sock = ch.socket();
        if(rcvbufSize != -1) {
            try {
                sock.setReceiveBufferSize(rcvbufSize);
            } catch (SocketException e) {
                LOG.error("Failed to setReceiveBufferSize.", e);
                throw new IllegalStateException(e);
            }
        }
        final boolean connected;
        try {
            connected = ch.connect(sockAddr);
        } catch (IOException e) {
            LOG.error("Failed to connect socket: " + sockAddr, e);
            throw new IllegalStateException(e);
        }
        if(!connected) {
View Full Code Here

Examples of java.nio.channels.SocketChannel

  }

  @Test
  public void sendGarbageTest() throws IOException {
    InetSocketAddress socketAddress = new InetSocketAddress(PORT);
    SocketChannel channel = SocketChannel.open(socketAddress);
    channel.write(
        ByteBuffer.wrap(
            new byte[] {1, 1, 1, 1// garbage
        )
    );
    channel.close();
  }
View Full Code Here

Examples of java.nio.channels.SocketChannel

    if (TRACE) trace("kryonet", "Connection listener removed: " + listener.getClass().getName());
  }

  void notifyConnected () {
    if (INFO) {
      SocketChannel socketChannel = tcp.socketChannel;
      if (socketChannel != null) {
        Socket socket = tcp.socketChannel.socket();
        if (socket != null) {
          InetSocketAddress remoteSocketAddress = (InetSocketAddress)socket.getRemoteSocketAddress();
          if (remoteSocketAddress != null) info("kryonet", this + " connected: " + remoteSocketAddress.getAddress());
View Full Code Here

Examples of java.nio.channels.SocketChannel

  /**
   * Returns the IP address and port of the remote end of the TCP connection, or null if this connection is not connected.
   */
  public InetSocketAddress getRemoteAddressTCP () {
    SocketChannel socketChannel = tcp.socketChannel;
    if (socketChannel != null) {
      Socket socket = tcp.socketChannel.socket();
      if (socket != null) {
        return (InetSocketAddress)socket.getRemoteSocketAddress();
      }
View Full Code Here

Examples of java.nio.channels.SocketChannel

    close();
    writeBuffer.clear();
    readBuffer.clear();
    readBuffer.flip();
    try {
      SocketChannel socketChannel = selector.provider().openSocketChannel();
      Socket socket = socketChannel.socket();
      socket.setTcpNoDelay(true);
      socket.setTrafficClass(IPTOS_LOWDELAY);
      socket.connect(remoteAddress, timeout); // Connect using blocking mode for simplicity.
      socketChannel.configureBlocking(false);
      this.socketChannel = socketChannel;

      selectionKey = socketChannel.register(selector, SelectionKey.OP_READ);
      selectionKey.attach(this);

      if (DEBUG) {
        debug("kryonet", "Port " + socketChannel.socket().getLocalPort() + "/TCP connected to: "
          + socketChannel.socket().getRemoteSocketAddress());
      }

      lastReadTime = lastWriteTime = System.currentTimeMillis();
    } catch (IOException ex) {
      close();
View Full Code Here

Examples of java.nio.channels.SocketChannel

      throw ioEx;
    }
  }

  public Object readObject (Connection connection) throws IOException {
    SocketChannel socketChannel = this.socketChannel;
    if (socketChannel == null) throw new SocketException("Connection is closed.");

    if (currentObjectLength == 0) {
      // Read the length of the next object from the socket.
      if (!IntSerializer.canRead(readBuffer, true)) {
        readBuffer.compact();
        int bytesRead = socketChannel.read(readBuffer);
        readBuffer.flip();
        if (bytesRead == -1) throw new SocketException("Connection is closed.");
        lastReadTime = System.currentTimeMillis();

        if (!IntSerializer.canRead(readBuffer, true)) return null;
      }
      currentObjectLength = IntSerializer.get(readBuffer, true);

      if (currentObjectLength <= 0) throw new SerializationException("Invalid object length: " + currentObjectLength);
      if (currentObjectLength > readBuffer.capacity())
        throw new SerializationException("Unable to read object larger than read buffer: " + currentObjectLength);
    }

    int length = currentObjectLength;
    if (readBuffer.remaining() < length) {
      // Read the bytes for the next object from the socket.
      readBuffer.compact();
      int bytesRead = socketChannel.read(readBuffer);
      readBuffer.flip();
      if (bytesRead == -1) throw new SocketException("Connection is closed.");
      lastReadTime = System.currentTimeMillis();

      if (readBuffer.remaining() < length) return null;
View Full Code Here

Examples of java.nio.channels.SocketChannel

      lastWriteTime = System.currentTimeMillis();
    }
  }

  private boolean writeToSocket (ByteBuffer buffer) throws IOException {
    SocketChannel socketChannel = this.socketChannel;
    if (socketChannel == null) throw new SocketException("Connection is closed.");

    while (buffer.hasRemaining()) {
      if (bufferPositionFix) {
        buffer.compact();
        buffer.flip();
      }
      if (socketChannel.write(buffer) == 0) break;
    }

    return !buffer.hasRemaining();
  }
View Full Code Here

Examples of java.nio.channels.SocketChannel

  /**
   * This method is thread safe.
   */
  public int send (Connection connection, Object object) throws IOException {
    SocketChannel socketChannel = this.socketChannel;
    if (socketChannel == null) throw new SocketException("Connection is closed.");
    synchronized (writeLock) {
      tempWriteBuffer.clear();
      tempWriteBuffer.position(5); // Allow room for the data length.

View Full Code Here

Examples of java.nio.channels.SocketChannel

          if ((ops & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
            ServerSocketChannel serverChannel = this.serverChannel;
            if (serverChannel == null) continue;
            try {
              SocketChannel socketChannel = serverChannel.accept();
              if (socketChannel != null) acceptOperation(socketChannel);
            } catch (IOException ex) {
              if (DEBUG) debug("kryonet", "Unable to accept new connection.", ex);
            }
            continue;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.