Go to mina2.0 Quick Start

Source: Internet
Author: User
Tags creative commons attribution

Minatimeserver. Java

Package com. Vista;

 

 

Import java. Io. ioexception;
Import java.net. inetsocketaddress;
Import java. NiO. charset. charset;

 

 

Import org. Apache. Mina. Core. Service. ioacceptor;
Import org. Apache. Mina. Core. session. idlestatus;
Import org. Apache. Mina. Filter. codec. protocolcodecfilter;
Import org. Apache. Mina. Filter. codec. textline. textlinecodecfactory;
Import org. Apache. Mina. Filter. Logging. loggingfilter;
Import org. Apache. Mina. Transport. Socket. NiO. niosocketacceptor;

 

 

Public class minatimeserver
{
Private Static final int Port = 6488;
Public static void main (string [] ARGs) throws ioexception
{
// Listen for the coming TCP Connection
Ioacceptor acceptor = new niosocketacceptor ();
Acceptor. getfilterchain (). addlast ("logger", new loggingfilter ());
Acceptor. getfilterchain (). addlast ("codec", new protocolcodecfilter (New textlinecodecfactory (charset. forname ("UTF-8 "))));

Acceptor. sethandler (New timeserverhandler ());

Acceptor. getsessionconfig (). setreadbuffersize (2048 );
Acceptor. getsessionconfig (). setidletime (idlestatus. both_idle, 10 );

Acceptor. BIND (New inetsocketaddress (port ));
System. Out. println ("server startup ");
}

 

}

Timeserverhandler. Java

Package com. Vista;

 

 

Import java. util. date;

 

 

Import org. Apache. Mina. Core. Service. iohandleradapter;
Import org. Apache. Mina. Core. session. idlestatus;
Import org. Apache. Mina. Core. session. iosession;

 

 

Public class timeserverhandler extends iohandleradapter
{

 

 

Public void exceptioncaught (iosession session, throwable cause) throws exception
{
Cause. printstacktrace ();
}
Public void messagereceived (iosession session, object message) throws exception
{
String strmsg = message. tostring ();
If (strmsg. Trim (). inclusignorecase ("quit "))
{
Session. Close ();
Return;
}
Date = new date ();
Session. Write (date. tostring ());
System. Out. println ("message written ");
}
Public void sessionidle (iosession session, idlestatus status) throws exception
{
System. Out. println ("idle" + session. getidlecount (Status ));
}

 

}

Test:

Client output

Server output

User @ myhost: ~> Telnet fig 9123
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape Character is '^]'.
Hello
Wed Oct 17 23:23:36 EDT 2007
Quit
Connection closed by foreign host.
User @ myhost: ~>

Mina time server started.
Message written...

References: 1. Mina V2.0 Quick Start Guide

Author: Dongting sangren

Source: http://phinecos.cnblogs.com/

This blog complies with the Creative Commons Attribution 3.0 License. If it is used for non-commercial purposes, you can reprint it freely, but please keep the original author information and article URL.

Related Article

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.