Mina2.0 user manual Chinese version-Chapter 2 Section 4 UDP server instance

Source: Internet
Author: User

(Supplement: as the manual is constantly being updated, it may change. The last update date is. It is strongly recommended that you read the original document)

This time, we will start with the code in the org. Apache. Mina. example. UDP package. To keep it simple, we will focus only on the sections related to Mina.

To build a UDP Service, we need to complete the following two points: 1. create a datagram datasync socket to listen to incoming client requests (see the code memorymonitor in the package. java) 2. create an iohandler to process the events generated by the Mina framework (see the code memorymonitorhandler in the package. java) below is the code (memorymonitor. java) segment:
NioDatagramAcceptor acceptor = new NioDatagramAcceptor();acceptor.setHandler(new MemoryMonitorHandler(this));

Here, we create a NiO datagram receiver niodatagramacceptor object to pass in client requests, and then set the Handler through sethandler. The next step is to add a LOG filter to the filter chain of the receiver. The LOG filter can be used to observe the running process of Mina. By generating logs of different levels, provides an in-depth understanding of the Mina operating mechanism.

DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();chain.addLast("logger", new LoggingFilter());

Next, let's take a look at some specific Code related to UDP transmission. Here we set the receiver reuse address. The last thing we need to do is to bind it to a port-where port is an int type variable:

DatagramSessionConfig dcfg = acceptor.getSessionConfig();dcfg.setReuseAddress(true);acceptor.bind(new InetSocketAddress(PORT));

Next, let's look at "iohandler implementation ":

The UPD server has three most important event methods:

  • Session Creation
  • Message receiving
  • Session closed

Next, let's take a closer look at the specific implementation of each step:

Session creation event:

@Overridepublic void sessionCreated(IoSession session) throws Exception {    SocketAddress remoteAddress = session.getRemoteAddress();    server.addClient(remoteAddress);}

The Code only calls the addclient () method. This operation only adds a tab representing the client tab on the UI of the server.

Message Receiving Event:

@Overridepublic void messageReceived(IoSession session, Object message) throws Exception {    if (message instanceof IoBuffer) {        IoBuffer buffer = (IoBuffer) message;        SocketAddress remoteAddress = session.getRemoteAddress();        server.recvUpdate(remoteAddress, buffer.getLong());    } }

In this event, it is mainly to dump the received message data and provide response processing. This method completes message processing and writes the response to the session and returns it to the client.

Session Close event:

@Overridepublic void sessionClosed(IoSession session) throws Exception {    System.out.println("Session closed...");    SocketAddress remoteAddress = session.getRemoteAddress();    server.removeClient(remoteAddress);}

The close event only removes the tab that represents the client connection from the server UI.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.