Developing a NIO-Based Client

This tip shows you how to develop a New I/O-based client. As you probably know, NIO offers many advantages such as speed and the ability to maintain a large number of concurrent connections. Here’s the code:

import*;import java.nio.*;import*;import java.util.*;import java.nio.channels.*;import java.nio.charset.*;public class ClientNIO{  public static void main(String args[])  {  InetSocketAddress ISA=null;  SocketChannel clientNIO=null;  Selector selector=null;  SelectionKey key=null;    int port=5000;    try{;     clientNIO.configureBlocking(false);          }catch(IOException e)        {System.out.println(e.getMessage());} try{    InetAddress addr=InetAddress.getByName("localhost");    ISA=new InetSocketAddress(addr,port);               }catch(UnknownHostException e)      {System.out.println(e.getMessage());}      try{    clientNIO.connect(ISA);     }catch(IOException e)      {System.out.println(getMessage());}      try{     selector =;      }catch(IOException e)          {System.out.println(e.getMessage());}       try{          SelectionKey clientKey=clientNIO.register(selector,SelectionKey.OP_CONNECT);      }catch(Exception e)                {System.out.println(e.getMessage());}             try{            while(>0)             {                                       Set keys=selector.selectedKeys();             Iterator iter=keys.iterator();             while(iter.hasNext())                  {                  key=(SelectionKey);                  iter.remove();                  SocketChannel channel=(SocketChannel);                  if((key.isValid())&&(key.isConnectable()))                      {                      if(channel.isConnectionPending())channel.finishConnect();                      ByteBuffer serverBuf = null;                                            System.out.println("Connected...");                      while(true)                           {                           serverBuf = ByteBuffer.wrap(new String("Answer me dear server ...").getBytes());                           channel.write(serverBuf);                                                                               serverBuf.clear();                                                   ByteBuffer clientBuf = ByteBuffer.allocateDirect(1024);                            clientBuf.clear();                  ;                                                                                                                       clientBuf.flip();                             Charset charset=Charset.forName("ISO-8859-1");                            CharsetDecoder decoder = charset.newDecoder();                              CharBuffer charBuffer = decoder.decode(clientBuf);                            System.out.println(charBuffer.toString());                            clientBuf.clear();                                             }                           }                   }                }     }catch(IOException e)        {System.out.println(e.getMessage());               try{        ;key.cancel();                  }catch(Exception ex)                     {System.out.println(e.getMessage());}}  }
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may