sblig

BlogJava 首页 新随笔 联系 聚合 管理
  10 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks

服务器端:
    

// 创建一个非阻塞的server端socket ,用NIO
		SocketAcceptor acceptor = new NioSocketAcceptor();

		// 创建接收数据的过滤器
		DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

		// 设定这个过滤器一行一行(\r\n)的读数据
		chain.addLast("myChin", new ProtocolCodecFilter(
				new TextLineCodecFactory()));

		//设定服务器端的消息处理器,一个SamplMinaServerHandler对象(自己实现)继承IoHandlerAdapter
		acceptor.setHandler(new IoHandlerAdapter(){
			//当一个客端端连结进入时
			@Override
			public void sessionOpened(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				System.out.println("incomming client : "+session.getRemoteAddress());
			}
			
			//当一个客户端关闭时
			@Override
			public void sessionClosed(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				System.out.println("on client disconnect : "+session.getRemoteAddress());
			}

			//当客户端发送的消息到达时
			@Override
			public void messageReceived(IoSession session, Object message)
					throws Exception {
				// TODO Auto-generated method stub
				String s =  (String)message;
				System.out.println("收到客户端发来的消息:"+s);
				//测试将消息回给客户端
				session.write(s+count);
				count ++;
			}
			private int count =0;
		});
		//端口号
		int bindPort= 9988;
		
		//绑定打开,启动服务器
		acceptor.bind(new InetSocketAddress(bindPort));
		
		System.out.println("Mina Server is listing on:="+bindPort);

  
   
   
  客户端:
   

// create TCP/IP connector
		NioSocketConnector connector = new NioSocketConnector();

		// 创建接收数据的过滤器
		DefaultIoFilterChainBuilder chain = connector.getFilterChain();

		// 设定这个过滤器将一行一行(/r/n)的读取数据
		chain.addLast("myChin", new ProtocolCodecFilter(
				new TextLineCodecFactory()));

		// 设定服务器端的消息处理器:一个SamplMinaServerHandler对象,
		connector.setHandler(new IoHandlerAdapter(){
			@Override
			public void messageReceived(IoSession session, Object message)
					throws Exception {
				// 我们己设定了服务器解析消息的规则是一行一行读取,这里就可转为String:
				String s = (String) message;
				// Write the received data back to remote peer
				System.out.println("服务器发来的收到消息: " + s);
				// 测试将消息回送给客户端
				session.write(s);
			}

			@Override
			public void sessionClosed(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				System.out.println("one Clinet Disconnect !");
			}

			@Override
			public void sessionOpened(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				System.out.println("incomming client  " + session.getRemoteAddress());
				session.write("我来啦........");
			}
		});
		
		// Set connect timeout.
		connector.setConnectTimeout(30);
		
		// 连结到服务器:
		ConnectFuture cf = connector.connect(new InetSocketAddress("localhost",
				9988));
		
		// Wait for the connection attempt to be finished.
		cf.awaitUninterruptibly();
		cf.getSession().getCloseFuture().awaitUninterruptibly();
		connector.dispose();

 
   
   
       



已有 0 人发表留言,猛击->>这里<<-参与讨论


ITeye推荐



posted on 2012-09-28 10:52 李凡 阅读(187) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: