This example shows how the client-server architecture is implemented using the example of cloud data storage.
ServerBootstrap b = new ServerBootstrap();
b.group(mainGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(
new ObjectDecoder(50 * 1024 * 1024, ClassResolvers.cacheDisabled(null)),
new ObjectEncoder(),
new AuthHandler(),
new MainHandler(),
new MessageHandler(),
new CommandHandler()
);
}
})
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture future = b.bind(8189).sync();
future.channel().closeFuture().sync();
public static void start() {
try {
socket = new Socket("localhost", 8189);
out = new ObjectEncoderOutputStream(socket.getOutputStream());
in = new ObjectDecoderInputStream(socket.getInputStream(), 50 * 1024 * 1024);
} catch (IOException e) {
e.printStackTrace();
}
}