java mina udp_MINA的UDP协议demo

importjava.net.InetSocketAddress;importjava.nio.charset.Charset;importorg.apache.log4j.Logger;importorg.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;importorg.apache.mina.core.future.ConnectFuture;importorg.apache.mina.core.future.IoFuture;importorg.apache.mina.core.future.IoFutureListener;importorg.apache.mina.core.service.IoHandlerAdapter;importorg.apache.mina.core.session.IoSession;importorg.apache.mina.filter.codec.ProtocolCodecFilter;importorg.apache.mina.filter.codec.textline.LineDelimiter;importorg.apache.mina.filter.codec.textline.TextLineCodecFactory;importorg.apache.mina.filter.logging.LoggingFilter;importorg.apache.mina.transport.socket.DatagramConnector;importorg.apache.mina.transport.socket.nio.NioDatagramConnector;publicclassClientextendsIoHandlerAdapter {privatestaticLogger logger=Logger.getLogger(Client.class);

DatagramConnector connector;

IoSession session;publicClient() {

connector=newNioDatagramConnector();

connector.setHandler(this);

DefaultIoFilterChainBuilder chain=connector.getFilterChain();

chain.addLast("logger",newLoggingFilter());

chain.addLast("codec",newProtocolCodecFilter(newTextLineCodecFactory(Charset.forName("UTF-8"),

LineDelimiter.NUL, LineDelimiter.NUL)));

IoFuture connFuture=connector.connect(newInetSocketAddress("127.0.0.1",1234));

connFuture.addListener(newIoFutureListener() {publicvoidoperationComplete(IoFuture future) {

ConnectFuture connFuture=(ConnectFuture) future;if(connFuture.isConnected()) {

session=future.getSession();try{

sendData();

}catch(InterruptedException e) {

e.printStackTrace();

}

}else{

logger.error("Not connected...exiting");

}

}

});

}privatevoidsendData()throwsInterruptedException {

session.write("测试数据发送!");

}publicstaticvoidmain(String[] args) {newClient();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值