Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:50:21+00:00 2026-06-11T20:50:21+00:00

I built a simple chat application using nio channels. I am very much new

  • 0

I built a simple chat application using nio channels. I am very much new to networking as well as threads. This application is for communicating with server (Server / Client chat application).

My problem is that multiple clients are not supported by the server.
How do I solve this problem?
What’s the bug in my code?

public class Clientcore extends Thread
{

    SelectionKey selkey=null;
    Selector sckt_manager=null;
    public void coreClient()
    {
       System.out.println("please enter the text");
       BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
       SocketChannel sc = null;
        try
        { sc = SocketChannel.open();
            sc.configureBlocking(false);       
            sc.connect(new InetSocketAddress(8888));  
            int i=0;
           while (!sc.finishConnect())
            {   
            } 
            for(int ii=0;ii>-22;ii++)
            {
                System.out.println("Enter the text");
                String HELLO_REQUEST =stdin.readLine().toString();
                if(HELLO_REQUEST.equalsIgnoreCase("end"))
                {
                    break;
                }
                System.out.println("Sending a request to HelloServer");    
                ByteBuffer buffer = ByteBuffer.wrap(HELLO_REQUEST.getBytes());    
                sc.write(buffer); 
            }   
        } 
        catch (IOException e) 
        {          
            e.printStackTrace();    
        }
        finally
        {       
            if (sc != null)
            {            
                try 
                {             
                    sc.close();            
                }
                catch (Exception e)
                {           
                    e.printStackTrace();       
                }       
            } 
            }   }

     public void run()
     {
      try
      {
        coreClient();
      }
      catch(Exception ej)
      {
         ej.printStackTrace();
      }}}

public class ServerCore extends Thread
{

    SelectionKey selkey=null;
    Selector sckt_manager=null;
      public void run()
     {
        try
        {
            coreServer();
        }
        catch(Exception ej)
        {
            ej.printStackTrace();
        }
     }

    private void coreServer() 
    {
        try
        {
            ServerSocketChannel ssc = ServerSocketChannel.open();
              try
                 {   
                    ssc.socket().bind(new InetSocketAddress(8888));   

                     while (true)
                     { 

                         sckt_manager=SelectorProvider.provider().openSelector();
                         ssc.configureBlocking(false);   
                         SocketChannel sc = ssc.accept();
                         register_server(ssc,SelectionKey.OP_ACCEPT);
                        if (sc == null) 
                         {   
                        } 
                         else
                            { 

                                System.out.println("Received an incoming connection from " + sc.socket().getRemoteSocketAddress()); 
                                printRequest(sc); 
                                System.err.println("testing 1");
                                String HELLO_REPLY = "Sample Display";
                                ByteBuffer buffer = ByteBuffer.wrap(HELLO_REPLY.getBytes());
                                System.err.println("testing 2");
                                sc.write(buffer); 
                                System.err.println("testing 3");
                                sc.close();
                            }}}
              catch (IOException e)
              { 
                   e.printStackTrace(); 
              }
               finally
              { 
                   if (ssc != null) 
                   { 
                    try 
                    { 
                            ssc.close(); 
                    }
                    catch (IOException e) 
                    { 
                            e.printStackTrace(); 
                    }
                   }
               }
        }
        catch(Exception E)
        {
            System.out.println("Ex in servCORE   "+E);
        }    
    }


    private static void printRequest(SocketChannel sc) throws IOException
          {

                ReadableByteChannel rbc = Channels.newChannel(sc.socket().getInputStream()); 
                 WritableByteChannel wbc = Channels.newChannel(System.out); 
                 ByteBuffer b = ByteBuffer.allocate(1024); // read 1024 bytes  
                 while (rbc.read(b) != -1) 
                 {
                    b.flip();
                    while (b.hasRemaining())
                    { 
                         wbc.write(b);
                         System.out.println();
                    }
                    b.clear();
                 }
          }
     public void register_server(ServerSocketChannel ssc,int selectionkey_ops)throws Exception
      {
        ssc.register(sckt_manager,selectionkey_ops);
       }}

public class HelloClient
{

  public void coreClientChat() 
    {
        Clientcore t=new Clientcore();
        new Thread(t).start();
    }

    public static void main(String[] args)throws Exception
    {     
       HelloClient cl= new HelloClient();
       cl.coreClientChat();
    }}
public class HelloServer
    {

          public void coreServerChat()
          {
              ServerCore t=new ServerCore();
              new Thread(t).start();
          }

          public static void main(String[] args)
          {    
             HelloServer st= new HelloServer();
             st.coreServerChat();

          }}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:50:23+00:00Added an answer on June 11, 2026 at 8:50 pm

    Perfect place for beginers
    Hello NIO Server

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple chat system I've built using PHP and MySQL (this
Hi I have built a simple WCF application I am using to query data
I have built a simple website monitoring application using Indy TIdhttp component. I want
I am thinking of a simple chat application in Window Phone using Microsoft Push
all. I have built a simple jQuery/PHP chat program that works rather well. However,
I built simple application in C# that is posting new link to google+ account,
I have built a simple StreamGeometry, using MSDN example: StreamGeometry geometry = new StreamGeometry();
I am trying to build a simple chat application using AMQP, Websockets and Ruby.
I built a very simple chat system... it works great and when the user
I'm just about to build a simple chat application using One Time Pad. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.