A simple example of a general understanding of the Mina framework, on the basis of the previous section, to see how to implement client-server communication,
Don't say much nonsense, directly look at the code:
public class Test {public
static void Main (string[] args) throws exception{
socketconnector connector = new Nioso Cketconnector ();
Iofilter filter = new Protocolcodecfilter (new Textlinecodecfactory ());
Connector.getfilterchain (). AddLast ("Vestigge", filter);
SocketAddress soketaddress = new Inetsocketaddress ("127.0.0.1", 5469);
Connector.sethandler (New ClientHandler ());
Connectfuture future= Connector.connect (soketaddress);
Future.join ();
if (!future.isconnected ()) {
System.out.println ("Connection Server failed");
return;
}
Future.getsession (). Write ("Hello");
You can see the code is very similar to the server-side code, and it is very simple, this is the benefits of the framework, no longer to reinvent the wheel, save a lot of things,
public class ClientHandler extends Iohandleradapter {public
void messagereceived (iosession arg0, Object message) THR oWS Exception {
System.out.println ("Received Server message:" + message.tostring ());
}
public void Exceptioncaught (Iosession arg0, Throwable arg1)
throws Exception {
}
}
Effect Demo: