Broadwell
Broadwell

Reputation: 1384

NettyIO disconnect Client from Server

How can I disconnect a netty client from the server so it executes the handerRemoved method on the server side and completely stops running? I tried using group.shutDownGraceFully() but the client still keeps connected to the server. Is there any method I am missing?

I also noticed when I try to connect to the server and it is not reachable (connection refused), the next time I connect to a real server it connects but it does not send or get any more messages.

Upvotes: 1

Views: 1786

Answers (1)

idiotprogrammer
idiotprogrammer

Reputation: 146

you seem to be new to network programming in general.

I am new to netty so please don't take anything I say as 100% true and especially not anywhere near 100% efficient.

so one major basic fact of network programming is that the the client and server are not directly linked (obviously). In order to execute a method on the server, you will need to send a message from the client to the server. for instance:

what you have on Client:

 //on shutdown
{
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

what you want:

{
        yourchannelname.writeAndFlush("bye"+"\r\n")
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

and when the server receives the bye command:

   // If user typed the 'bye' command, wait until the server closes
            // the connection.
            if ("bye".equals(line.toLowerCase())) {
                ch.closeFuture().sync();
                break;
            }
        }
        //this is for safety reasons, it is optional-ish

        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
      //what you already have
    } finally {
        // The connection is closed automatically on shutdown.
        group.shutdownGracefully();
    }
}

hope this helped, iv never answered a question on stack overflow before so I hope I at least sound like I know what im doing :P

Upvotes: 1

Related Questions