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 734357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:21:51+00:00 2026-05-14T07:21:51+00:00

I am trying to learn Java. I would like to implement a simple networked

  • 0

I am trying to learn Java. I would like to implement a simple networked connect 4 game as well as a chat feature.

I want my network logic to be non blocking so after much study I found that SocketChannel is what I am after regrading my needs.

What has not made sense still is the lack of CallBack functions in SocketChannels.. Like one finds in C#.

My query for this time is: How do I deliver the data received to the Chat or Game form (JFrame)?

Some guidance is most welcome.

  • 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-05-14T07:21:51+00:00Added an answer on May 14, 2026 at 7:21 am

    You need to use a Selector. First create a Selector to receive the events:

    Selector selector = Selector.open()
    

    Then you need to register the ServerSocketChannel with the selector:

    SelectionKey acceptKey = server.register(selector, SelectionKey.OP_ACCEPT);
    

    Then you need to use the Selector to process events as they come in (you can think of this as the “callback” part of the process:

    while(true){
      //how many channel keys are available
      int available = selector.select(); 
      //select is blocking, but should only return if available is >0, this is more of a sanity check
      if(available == 0) continue;
    
      Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
      while(keys.hasNext()){
        SelectionKey key = keys.next();
        keys.remove();
        //someone is trying to connect to the server socket
        if(key.isAcceptable())  doAccept(key); 
        //someone is sending us data
        else if(key.isReadable()) doRead(key); 
        //we are trying to (and can) send data
        else if(key.isWritable()) doWrite(key);
    }
    

    The meat will be in doAccept(), doRead(), and doWrite(). For an accept key the selection key will contain the information to create the new Socket.

    doAccept(SelectionKey key){
    
    //create the new socket
    SocketChannel socket = ((ServerSocketChannel)key.channel()).accept(); 
    //make it non-blocking as well
    socket.configureBlocking(false);
    
    ...
    //here you would likely have some code to init your game objects / communication protocol, etc. and generate an identifier object (used below).
    //and be able to find the socket created above
    ...
    
    //Since it is non blocking it needs a selector as well, and we register for both read and write events
    SelectionKey socketKey = socket.register(selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE);
    // so we can identify the events as they come in
    socketKey.attach(someSocketIndentifier);
    }
    

    The last line adds some object to the key so that the events received from the selector can be attributed to a connection (for example it might be a player in your game). So now you can accept new connections and you will just need to read and write.

    doRead(SelectionKey key){
      //here we retrieve the key we attached earlier, so we now what to do / wheer the data is coming from
      MyIdentifierType myIdentifier = (MyIdentifierType)key.attachment();
      //This is then used to get back to the SocketChannel and Read the Data
      myIdentifier.readTheData();
    }
    

    similarly for write

    doWrite(SelectionKey key){
      //here we retrieve the key we attached earlier, so we now what to do / wheer the data is coming from
      MyIdentifierType myIdentifier = (MyIdentifierType)key.attachment();
      //This is then used to get back to the SocketChannel and Read the Data
      myIdentifier.getSocketHandler().writePendingData();
    }
    

    Reading is fairly straight forward, you just create a ByteBuffer and then call the SocketChannels read(ByteBuffer) (or one of its variants) to get the data ready on the channel until its empty.

    Writing is a bit trickier as you will usually want to buffer the data to be written until you recieve the write event:

    class MyNetworkClass{
      ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
      SocketChannel commchannel; //from the server accept processing
    
      ...
    
      public void write(byte[] data){
        //here the class writeBuffer object is filled with the data
        //but it isn't actually sent over the socket
        ...
      }
    
      public void writePendingData(){
        //here actually write the data to the socket
        commchannel.write(writeBuffer);
      }
    }
    

    Note that you will need appropriate code to manage the buffer in the class in the event it becomes full, or to modify it appropriately in the write pending method if not all of the data in the buffer is written out to the socket, as well as the various exceptions that can be thrown during the process. Hope this helps to get you started.

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

Sidebar

Related Questions

greetngs, i am trying to learn Java and Swing by writing a simple game
I was trying to learn a little about JAVA frameworks like Spring. I hit
I'm trying to learn servlets and JSP and would like to do this with
I was trying to implement a Matrix.class to learn some Java. Right now, I
I'm trying to learn Scala, and I'd like to learn by writing a simple
Greetings. I am trying to learn Java and Swing (today is my first day).
I'm trying to learn web development in Java and, from what I have seen
I'm trying to learn how to do passphrase-based encryption with Java. I'm finding several
I've been lately trying to learn more and generally test Java's serialization for both
I've developed in Java in the past, and now I'm trying to learn Grails/Groovy

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.