mina服务器、客户端


服务器程序:

public class Server {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NioSocketAcceptor nioSocketAcceptor = new NioSocketAcceptor();
		nioSocketAcceptor.setDefaultLocalAddress(new InetSocketAddress(5222));
		nioSocketAcceptor.setHandler(new ServerIoHandler());//引入IoHandler
		
		DefaultIoFilterChainBuilder defaultIoFilterChainBuilder = new DefaultIoFilterChainBuilder();
		defaultIoFilterChainBuilder.addLast("executor", new ExecutorFilter());//firing event
		//defaultIoFilterChainBuilder.addLast("codec", new ProtocolCodecFilter(new ServerCodecFactory()));//excute 执行iohandle操作
		defaultIoFilterChainBuilder.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));//excute 执行iohandle操作
		
		nioSocketAcceptor.setFilterChainBuilder(defaultIoFilterChainBuilder);
		try {
			nioSocketAcceptor.bind();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

ServerIoHandler.java

public class ServerIoHandler implements IoHandler{

	@Override
	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		String str = message.toString();  
        if (str.trim().equalsIgnoreCase("quit")) {  
            session.close();  
            return;  
        }  
        Date date = new Date();  
        System.out.println("hello"+str+session.getRemoteAddress()+date.toString());  
          
        session.write("i am recived");  
        System.out.println("Message written...");  
  
	}

	@Override
	public void messageSent(IoSession arg0, Object arg1) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionClosed(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("客户端与服务端断开连接.....");  
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("新服务器IP地址:"+session.getRemoteAddress().toString());
	}

}

客户端:

public class ClientB {

	public static void main(String[] args) {
		
		NioSocketConnector connector = new NioSocketConnector();     
		//创建接受数据的过滤器     
		DefaultIoFilterChainBuilder chain = connector.getFilterChain();     
		//设定这个过滤器将一行一行(/r/n)的读取数据     
		chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));     
		//客户端的消息处理器:一个SamplMinaServerHander对象     
		connector.setHandler(new MinaClientHandler());     
		//set connect timeout     
		connector.setConnectTimeout(30);     
		//连接到服务器:     
		ConnectFuture cf = connector.connect(new InetSocketAddress("172.16.16.121",5222));     
		
		//Wait for the connection attempt to be finished.     
		cf.awaitUninterruptibly();     
		
		System.out.println("本地IP信息:"+cf.getSession().getLocalAddress());
		String port = cf.getSession().getLocalAddress().toString().split(":")[1];
		System.out.println("端口号:"+port);
		
		//startListener(Integer.parseInt(port));
		
		cf.getSession().getCloseFuture().awaitUninterruptibly();  
		connector.dispose();
   
	<pre name="code" class="html">}
}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值