Java Code Examples for java.nio.channels.SocketChannel#getLocalAddress()

The following examples show how to use java.nio.channels.SocketChannel#getLocalAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SocketChannelWrapperBar.java    From baratine with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the server inet address that accepted the request.
 */
@Override
public InetAddress addressLocal()
{
  SocketChannel s = _channel;
  
  if (s != null) {
    try {
      InetSocketAddress addr = (InetSocketAddress) s.getLocalAddress();
      
      return addr.getAddress();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  else {
    return null;
  }
}
 
Example 2
Source File: SocketChannelWrapperBar.java    From baratine with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the server inet address that accepted the request.
 */
@Override
public InetSocketAddress ipLocal()
{
  SocketChannel s = _channel;
  
  if (s != null) {
    try {
      return (InetSocketAddress) s.getLocalAddress();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  else {
    return null;
  }
}
 
Example 3
Source File: SocketChannelWrapperBar.java    From baratine with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the server port that accepted the request.
 */
@Override
public int portLocal()
{
  SocketChannel s = _channel;
  
  if (s != null) {
    try {
      InetSocketAddress addr = (InetSocketAddress) s.getLocalAddress();
      
      return addr.getPort();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  else {
    return -1;
  }
}
 
Example 4
Source File: TestUtil.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
public static InetAddress getLocalAdress(String remoteHost, int port) throws IOException {
    InetSocketAddress socketAddr = new InetSocketAddress(remoteHost, port);
    SocketChannel socketChannel = SocketChannel.open(socketAddr);
    InetSocketAddress localAddress = (InetSocketAddress) socketChannel.getLocalAddress();
    return localAddress.getAddress();
}
 
Example 5
Source File: VNCScreen.java    From Monocle with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void run() {
    ByteBuffer buffer = ByteBuffer.allocate(64);
    buffer.order(ByteOrder.BIG_ENDIAN);
    while (true) {
        try {
            SocketChannel client = server.accept();
            System.out.format("Connection received from %s\n",
                              client.getRemoteAddress());
            // Declare the server protocol version
            buffer.clear();
            buffer.put("RFB 003.003\n".getBytes());
            buffer.flip();
            client.write(buffer);
            // Read the client protocol version
            buffer.clear();
            buffer.limit(12);
            client.read(buffer);
            buffer.flip();
            System.out.format("Client supports %s\n",
                              Charset.forName("UTF-8")
                                      .decode(buffer).toString().trim());
            buffer.clear();
            buffer.putInt(1); // no authentication
            buffer.flip();
            client.write(buffer);
            buffer.clear();
            buffer.limit(1);
            client.read(buffer);
            System.out.format("Client share request: %d\n",
                              buffer.get(0));
            buffer.clear();
            buffer.putShort((short) width);
            buffer.putShort((short) height);
            buffer.put((byte) depth);
            buffer.put((byte) depth);
            buffer.put((byte) (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN) ? 0 : 1));
            buffer.put((byte) 1); // true color
            if (depth == 32) {
                buffer.putShort((short) 255); // red max
                buffer.putShort((short) 255); // green max
                buffer.putShort((short) 255); // blue max
                buffer.put((byte) 16); // red offset
                buffer.put((byte) 8); // blue offset
                buffer.put((byte) 0); // green offset
            } else {
                buffer.putShort((byte) (short) 31);
                buffer.putShort((byte) (short) 63);
                buffer.putShort((byte) (short) 31);
                buffer.put((byte) 11);
                buffer.put((byte) 5);
                buffer.put((byte) 0);
            }
            buffer.put((byte) 0); // padding
            buffer.put((byte) 0);
            buffer.put((byte) 0);
            String name = "JavaFX on " + client.getLocalAddress();
            buffer.putInt(name.length());
            buffer.put(name.getBytes());
            buffer.flip();
            client.write(buffer);
            ClientConnection cc = new ClientConnection();
            cc.socket = client;
            Thread t = new Thread(cc);
            t.setDaemon(true);
            t.setName("VNC client connection from "
                              + client.getRemoteAddress());
            t.start();
            synchronized (clients) {
                clients.add(cc);
            }
            sendBuffer(client);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}