writing in AsynchronousSocketChannel
I want to respond to browser when browse localhost:8080 but nothing happen
what should i do and what is mt program problem.
this is my complete code: it accept browser connection successfully but
nothing happen!
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class SufeSurf {
public static void main(String[] args) {
int port = 8080;
try {
final AsynchronousChannelGroup group =
AsynchronousChannelGroup.withThreadPool(Executors
.newSingleThreadExecutor());
final AsynchronousServerSocketChannel server =
AsynchronousServerSocketChannel.open(group).bind(
new InetSocketAddress(port));
System.out.println("Server listening on " + port);
server.accept("Client connection",
new CompletionHandler<AsynchronousSocketChannel,
Object>() {
public void completed(AsynchronousSocketChannel ch, Object
att) {
System.out.println("Accepted a connection");
ch.write(ByteBuffer.wrap("aaa".getBytes()));
// accept the next connection
server.accept("Client connection", this);
}
public void failed(Throwable exc, Object att) {
System.out.println("Failed to accept connection");
}
});
group.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
} catch (IOException | InterruptedException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
any help apperciated!
No comments:
Post a Comment