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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:30:56+00:00 2026-06-05T16:30:56+00:00

I have a question about ClientBootStrap. Here is the scenario; Client_X wants to join

  • 0

I have a question about ClientBootStrap.
Here is the scenario;

  1. Client_X wants to join Server_A.
  2. But the Server_A somehow wants the Client_x to join in Server_B.
  3. So Server_A sends Server_B’s info to Client_X for RECONNECTION
  4. As soon as the Client_X gets the RECONNECTION message, he tries disconnecting from Server_A and tries to connecting to Server_B.
    But it fails. Because as soon as Client disconnects from Server_A, he cannot use the disconnected channel anymore.

This looks simple. But here is my implementation

    @Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)throws Exception  {
    if(e.getMessage() instanceof SomePacket){
    ....
    }else if(e.getMessage() instanceof Reconnect){ //Server_A has sent a RECONNECT Message.(Redirection)
        Reconnect theReconnectPacket = (Reconnect)e.getMessage();
        String hostname = theReconnectPacket.getHostname();
        int port = theReconnectPacket.getPort();

        this.reconnectHost  = hostname;
    this.reconnectPort  = port;
    this.currentState   = 1;

    ctx.getChannel().disconnect();    //DISCONNECT FROM SERVER_A

     }
}

@Override
public void channelDisconnected(ChannelHandlerContext ctx,ChannelStateEvent e) throws Exception {

     if(this.currentState == 1){

        Channel disconnectedChannel = ctx.getChannel();

    if (!disconnectedChannel.isConnected()){

    SocketAddress destinationAddress = new InetSocketAddress(this.reconnectHost, this.reconnectPort);
           //TRYING TO RECONNECT SERVER_B
    disconnectedChannel.connect(destinationAddress);    //**Error Line:java.nio.channels.ClosedChannelException**

    }

      }


    super.channelDisconnected(ctx, e);
}

As you can see in the Error Line, I got this exception:java.nio.channels.ClosedChannelException. Couldn’t we use the same channel after it is disconnected?. Once it is disconnected, is it done?. How could we recreate a connection in the SimpleChannelHandler ?

Thanks for further comments 🙂

<<<<< NEW APPROACH >>>>>>

Ok. So in the SimpleChannledHandler , I use ClientBootStrap to connect a differentPort.

 @Override
public void channelDisconnected(ChannelHandlerContext ctx,ChannelStateEvent e) throws Exception {

       Channel disconnectedChannel = ctx.getChannel();
    final ClientDataObject oldObject = ClientDataState.clientObject.get(disconnectedChannel);

    if(oldObject.getClientState() == 1){

        if (!disconnectedChannel.isConnected()){

SocketAddress destinationAddress = new InetSocketAddress(this.reconnectHost, this.reconnectPort);

ChannelFuture connectFuture = bootstrap.connect(destinationAddress);

connectFuture.addListener(new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {

        Channel newChannel = channelFuture.getChannel();
    ClientDataObject newObject =  ClientDataState.clientObject.get(newChannel);
                      newObject.setClientID(oldObject.getClientID());


                     newObject.setClientState(oldObject.getClientState());
                    newObject.setRoomId(oldObject.getRoomId());
                    newObject.setClientState(1);

                    ClientDataState.clientObject.set(newChannel, newObject);

                    Channels.write(newChannel, new Login(newObject.getClientID()));
               }});             

        }else{
                     //Channled connected
            }

    }

    super.channelDisconnected(ctx, e);

}

But I need to know some information of Client_X. As soon as the Client_x is disconnected the pipeline create another SimpleChannelHandler. So all my information is gone. I try to use ChannelLocal to keep state of the client. But it is also useless since it is related with channel object. When I connect to newChannel I cannot use old SimpleChannelHandlers’s data again. (like clientID,roomID etc.)

My point is how to store information without being effected by channel(session), I want to access the data from any channel handler.

The way to handle this question should we implement ChannelPipelineFactory like this?

   public class GameClientPipelineFactory implements ChannelPipelineFactory{

      private static final ClientStaticHandler SHARED = new ClientStaticHandler();  

           private Someobject o;
public GameClientPipelineFactory(Someobject refTOSomeObject) {
    super();
    this.o = refToSomeObject;
}

@Override
public ChannelPipeline getPipeline() throws Exception {
    // TODO Auto-generated method stub
    ChannelPipeline pipeline = Channels.pipeline();

    //pipeline.addLast("delimiter", new DelimiterBasedFrameDecoder(256, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new GameClientDecoder());
    pipeline.addLast("encoder", new GameClientEncoder());
    pipeline.addLast("shared", SHARED); //I added this line

    pipeline.addLast("logicHandler", new GameClientLogicHandler(this.o));

    return pipeline;

}

But then how am I gonna use this ‘shared’ handler? Each time when I need a global object, should I ask to pipeline to get this handler and get any object from ‘shared’ handler? Isn’t this a long way?

  • 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-05T16:30:57+00:00Added an answer on June 5, 2026 at 4:30 pm

    Instead of trying to reopen a closed channel, try creating a new channel.

    You may want to implement a callback to initiate a the creation of a new channel.

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

Sidebar

Related Questions

I have question about my code which does the Hoare Partition Method. Here is
I saw a lot of examples of CopyStream implementation but I have question about
Have a question about the design/usage of asp.net mvc here. In the html helper
I have question about Dictionaries in Python. here it is: I have a dict
i have a question about fopen() and base64 comunication. The scenario is that: i
Have a question about full text search. I have a query that works, but
I have question about try, catch and finally in Java. Consider the following scenario:
I have a question about this script I found and used. It works but
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass

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.