Java Socket framework Apache MINA: implement Socket server

Keywords: Session Apache socket Java

Java Socket framework Apache MINA: implement Socket server

Now, Apache MINA is used to realize a simple Socket Server. The Server implements a simple function. When a Socket client connects, it sends a simple string "zhangphil" to the client. The Server-side program code is as follows:

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
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 MINATest {
	public static void main(String[] args) {
		try {
			MINATest test = new MINATest();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public MINATest() throws Exception {
		IoAcceptor acceptor = new NioSocketAcceptor();

		// Filter chain.
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

		// Handle.
		acceptor.setHandler(new SocketServerHandler());

		// The server binds port 80 and waits for the client connection request.
		acceptor.bind(new InetSocketAddress(80));
	}

	// The Socket server side of Apache MINA.
	private class SocketServerHandler extends IoHandlerAdapter {

		// Session creation.
		@Override
		public void sessionCreated(IoSession session) throws Exception {
			super.sessionCreated(session);
			System.out.println("#sessionCreated#");
		}

		// Triggered when the session is opened (the sessionCreated function is triggered first when the first connection is made, and then this function is triggered).
		@Override
		public void sessionOpened(IoSession session) throws Exception {
			super.sessionOpened(session);
			System.out.println("#sessionOpened#");

			sendDataToClient(session);
		}

		@Override
		public void messageSent(IoSession session, Object message) throws Exception {
			super.messageSent(session, message);
			System.out.println("#messageSent#");

			System.out.println("=============");
			System.out.println(message.toString());
			System.out.println("=============");
		}

		// Triggered when a message is received.
		@Override
		public void messageReceived(IoSession session, Object message) throws Exception {
			super.messageReceived(session, message);
			// System.out.println("messageReceived");

			System.out.print(message.toString());
		}

		@Override
		public void sessionClosed(IoSession session) throws Exception {
			super.sessionClosed(session);
			System.out.println("\n#sessionClosed#");
		}

		@Override
		public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
			super.sessionIdle(session, status);
			System.out.println("#sessionIdle#");
		}

		@Override
		public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
			super.exceptionCaught(session, cause);
			System.out.println("#exceptionCaught#");
			cause.printStackTrace();
		}
	}

	private void sendDataToClient(IoSession session) {
		String string = "zhangphil";
		session.write(string);
	}
}

 

After the server-side program runs, when a client Socket is connected, the server-side program outputs the following results:

 

 

When the remote connected client suddenly disconnects, the server program outputs the following results:

 

At this time, the server-side program does not exit. When a new client Socket is connected, the server-side program, as always, sends the data "zhangphil" to the client. The output is as follows:

 

Posted by Xo_ on Sat, 30 Nov 2019 05:05:33 -0800