Example usage for java.nio.channels AsynchronousSocketChannel getRemoteAddress

List of usage examples for java.nio.channels AsynchronousSocketChannel getRemoteAddress

Introduction

In this page you can find the example usage for java.nio.channels AsynchronousSocketChannel getRemoteAddress.

Prototype

public abstract SocketAddress getRemoteAddress() throws IOException;

Source Link

Document

Returns the remote address to which this channel's socket is connected.

Usage

From source file:Main.java

@Override
public void completed(AsynchronousSocketChannel client, Attachment attach) {
    try {//w  w w  . ja v  a2s .  co m
        SocketAddress clientAddr = client.getRemoteAddress();
        System.out.format("Accepted a  connection from  %s%n", clientAddr);
        attach.server.accept(attach, this);
        ReadWriteHandler rwHandler = new ReadWriteHandler();
        Attachment newAttach = new Attachment();
        newAttach.server = attach.server;
        newAttach.client = client;
        newAttach.buffer = ByteBuffer.allocate(2048);
        newAttach.isRead = true;
        newAttach.clientAddr = clientAddr;
        client.read(newAttach.buffer, newAttach, rwHandler);
    } catch (IOException e) {
        e.printStackTrace();
    }
}