Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package example;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.nio.ByteBuffer;
- import java.nio.channels.AsynchronousServerSocketChannel;
- import java.nio.channels.AsynchronousSocketChannel;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.Future;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.TimeoutException;
- public class Server {
- public static void main(String[] args) {
- try (AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open()) {
- server.bind(new InetSocketAddress("127.0.0.1", 1234));
- Future<AsynchronousSocketChannel> acceptCon = server.accept();
- AsynchronousSocketChannel client = acceptCon.get(10, TimeUnit.SECONDS);
- if ((client != null) && (client.isOpen())) {
- ByteBuffer buffer = ByteBuffer.allocate(1024);
- Future<Integer> readval = client.read(buffer);
- System.out.println("Received from client: " + new String(buffer.array()).trim());
- readval.get();
- buffer.flip();
- String str = "I'm fine. Thank you!";
- Future<Integer> writeVal = client.write(ByteBuffer.wrap(str.getBytes()));
- System.out.println("Writing back to client: " + str);
- writeVal.get();
- buffer.clear();
- client.close();
- }
- } catch (ExecutionException | InterruptedException | TimeoutException | IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement