Knowing the basic concepts of Netty should make it easier to develop. In the "Hello World" code, we just print simple information in their respective local areas when the binding is complete, without client and server messaging. This is definitely the most basic feature. Before you start coding, add an important concept from Netty, ChannelBuffer.
ChannelBuffer
Messaging in Netty must be delivered in bytes in the ChannelBuffer. In a nutshell, you want to just write a string over there, sorry, throw an exception. Although the writer interface parameter defined by Netty is an Object, this can be a source of confusion for new users. In the source code of Netty, it is judged as follows:
SendBuffer acquire Object (the message) {
If (message instanceof ChannelBuffer) {
Return acquire ((ChannelBuffer) message);
} else if (message instanceof FileRegion) {
Return acquire ((FileRegion) message);
}
Throw new IllegalArgumentException -
"Unsupported message type:" + message.getclass ());
}
So, if we want to pass a string, we have to convert it to ChannelBuffer. With that in mind, here's the code:
/ * *
* @ author lihzh
* @ alia OneCoder
* @ blog http://www.coderli.com
* /
Public class the MessageServer {
Public static void main(String args[]) {
// Server service starter
ServerBootstrap bootstrap = new ServerBootstrap(
New NioServerSocketChannelFactory (
Executors. NewCachedThreadPool (),
Executors. NewCachedThreadPool ()));
// set up a class (Handler) to handle client messages and various message events.
The bootstrap. SetPipelineFactory (new ChannelPipelineFactory () {
@ Override
Public ChannelPipeline getPipeline() throws Exception {
Return Channels. Pipeline (new MessageServerHandler ());
}
});
// open 8000 ports for client access.
The bootstrap. Bind (new InetSocketAddress (8000));
}
Private static class MessageServerHandler extends SimpleChannelHandler {
/ * *
* the user receives a message from the client and triggers it when a client message arrives
*
* @ author lihzh
* @ alia OneCoder
* /
@ Override
Public void messageReceived(ChannelHandlerContext CTX, MessageEvent e) {
ChannelBuffer = (ChannelBuffer) etMessage();
System. The out. Println (buffer. ToString (Charset. DefaultCharset ()));
}
}
}
/ * *
* @ author lihzh
* @ alia OneCoder
* @ blog http://www.coderli.com
* /
Public class MessageClient {
Public static void main(String args[]) {
// Client service starter
ClientBootstrap bootstrap = new ClientBootstrap(
New NioClientSocketChannelFactory (
Executors. NewCachedThreadPool (),
Executors. NewCachedThreadPool ()));
// set up a class (Handler) to handle server-side messages and various message events.
The bootstrap. SetPipelineFactory (new ChannelPipelineFactory () {
@ Override
Public ChannelPipeline getPipeline() throws Exception {
Return Channels. Pipeline (new MessageClientHandler ());
}
});
// connect to the server on the local 8000 port
The bootstrap. Connect (new InetSocketAddress (8000) "127.0.0.1,");
}
Private static class MessageClientHandler extends SimpleChannelHandler {
/ * *
* triggered when bound to the server, sends a message to the server.
*
* @ alia OneCoder
* @ author lihzh
* /
@ Override
Public void channelConnected (ChannelHandlerContext CTX,
ChannelStateEvent e) {
// passes the string, constructed as ChannelBuffer, to the server
String MSG = "Hello, I'm client.";
ChannelBuffer buffer = ChannelBuffers. Buffer (MSG) length ());
Buffer. WriteBytes (MSG) getBytes ());
Um participant etChannel (), write (buffer);
}
}
}
Unlike the "Hello World" sample code, instead of printing the message locally after the channel is connected, the client converts the message into a ChannelBuffer and passes it to the server, which receives the ChannelBuffer, decodes it and prints it as a string. At the same time, by comparison, we can see that what has changed is only the code in the Handler, and the code that starts the service and binds the service has not changed, which is what we mentioned in the concept introduction, focus on the Handler, handle our own business in the Handler. So, in the future we will only show the key code in the business, not the code that will be repeated above :)
Since messaging in Netty is all ChannelBuffer, the use of ChannelBuffer will be covered in detail in the next chapter. We study together.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.