Developing a NIO Server

As you probably know, NIO offers many advantages?like speed and a large number of concurrent connections:

import*;import*;import java.nio.*;import java.util.*;import java.nio.channels.*;import java.nio.charset.*;public class ServerNIO{  public static void main(String[] args){  ServerSocketChannel serverNIO=null;  InetSocketAddress ISA=null;  Selector selector=null;  ByteBuffer clientBuf=null;  int port=5000;try{;   serverNIO.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{   ServerSocket SS=serverNIO.socket();   SS.bind(ISA);   System.out.println("- Ready -");   }catch(IOException e)      {System.out.println(e.getMessage());}try{;   serverNIO.register(selector,SelectionKey.OP_ACCEPT);   }catch(IOException e)      {System.out.println(e.getMessage());}     while(true)          {try{    ;              }catch(IOException e)                 {System.out.println(e.getMessage());}                      Set keys=selector.selectedKeys();       Iterator iter=keys.iterator();       while(iter.hasNext())            {            SelectionKey key=(SelectionKey);            iter.remove();                        if((key.isValid())&&(key.isAcceptable()))             {                   try{                  ServerSocketChannel SSC=(ServerSocketChannel);                                                                          SocketChannel clientNIO=SSC.accept();                             clientNIO.configureBlocking(false);          SelectionKey SK=clientNIO.register(            selector,SelectionKey.OP_READ | SelectionKey.OP_WRITE);                                                                                                                }catch(IOException e)                         {System.out.println(e.getMessage());}                                                         }                        if((key.isValid())&&(key.isReadable()))             {             try{                                SocketChannel clientNIO=(SocketChannel);                            clientBuf=ByteBuffer.allocateDirect(1024);                   ;                }catch(IOException e)                  {System.out.println(e.getMessage());                        try{                 ;key.cancel();                           }catch(IOException ex)                              {System.out.println(e.getMessage());}}                                                                           clientBuf.flip();                 Charset charset=Charset.forName("ISO-8859-1");                 CharsetDecoder decoder = charset.newDecoder();                 try{                    CharBuffer charBuffer = decoder.decode(clientBuf);                    System.out.println(charBuffer.toString());                    clientBuf.clear();                                                              }catch(IOException e)                       {System.out.println(e.getMessage());}                                        }                       if((key.isValid())&&(key.isWritable()))               {               SocketChannel clientNIO=(SocketChannel);               ByteBuffer serverBuf=ByteBuffer.allocateDirect(1024);                try{                  String s="Message from server ...";                  serverBuf.put(s.getBytes());                   serverBuf.flip();                  clientNIO.write(serverBuf);                                                                              }catch(IOException e)                    {System.out.println(e.getMessage());                    try{             ;key.cancel();                       }catch(IOException ex)                          {System.out.println(ex.getMessage());}}             }                                     }                                          }                                            }                                                               }
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

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

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.