Java Socket框架Apache MINA:实现Socket服务器端

版权声明:本文为Zhang Phil原创文章,请不要转载! https://blog.csdn.net/zhangphil/article/details/88247632

Java Socket框架Apache MINA:实现Socket服务器端 

现在用Apache MINA实现一个简单的Socket服务器端Server,服务器端实现一个简单的功能,当有Socket客户端连接过来后,发送一个简单的字符串“zhangphil”给客户端。服务器端程序代码如下:

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();

		// 过滤链。
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

		// 处理。
		acceptor.setHandler(new SocketServerHandler());

		// 服务器端绑定80端口,等待客户端连接请求。
		acceptor.bind(new InetSocketAddress(80));
	}

	// Apache MINA的Socket服务器端。
	private class SocketServerHandler extends IoHandlerAdapter {

		// 会话创建。
		@Override
		public void sessionCreated(IoSession session) throws Exception {
			super.sessionCreated(session);
			System.out.println("#sessionCreated#");
		}

		// 会话打开时触发(第一次连接时先触发sessionCreated函数,后触发本函数)。
		@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("=============");
		}

		// 接收到消息时触发。
		@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);
	}
}

服务器端程序运行后,当有客户端Socket连接过来,服务器端程序输出结果:

当远程连接过来的客户端突然断开连接,服务器端程序输出结果:

此时服务器端程序并未退出,当有新的客户端Socket连接过来后,服务端程序一如既往的发送数据“zhangphil”给客户端。输出如下:

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/88247632