我是靠谱客的博主 动人御姐,这篇文章主要介绍java udp mina,用MINA实现UDP通信的例子,现在分享给大家,希望可以做个参考。

1、UDPClient.java

import java.net.InetSocketAddress;

import java.nio.charset.Charset;

import org.apache.mina.core.future.ConnectFuture;

import org.apache.mina.core.service.IoConnector;

import org.apache.mina.core.session.IoSession;

import org.apache.mina.filter.codec.ProtocolCodecFilter;

import org.apache.mina.filter.codec.textline.LineDelimiter;

import org.apache.mina.filter.codec.textline.TextLineCodecFactory;

import org.apache.mina.transport.socket.nio.NioDatagramConnector;

/**

* @description

* @date:(2015-11-29 上午9:58:30)

* @author Administrator

* @version v1.0

* @since v1.0

*

* Modified history

*

* Modified date:

* Modifier user:

* description:

*

* */

public class UDPClient {

private InetSocketAddress inetSocketAddress;

private IoSession session;

private IoConnector connector;

public UDPClient() {

super();

}

public UDPClient(String host,int port){

inetSocketAddress=new InetSocketAddress(host,port);

}

public static void main(String[] args) {

UDPClient udpClient=new UDPClient("127.0.0.1",9897);

udpClient.setConnector(new NioDatagramConnector());

udpClient.getConnector().setHandler(new ClientIoHandler());

IoConnector connector=udpClient.getConnector();

connector.getFilterChain().addLast("codec",

new ProtocolCodecFilter(

new TextLineCodecFactory(

Charset.forName("UTF-8"),

LineDelimiter.WINDOWS.getValue(),

LineDelimiter.WINDOWS.getValue())));

ConnectFuture connectFuture=connector.connect(udpClient.getInetSocketAddress());

// 等待是否连接成功,相当于是转异步执行为同步执行。

connectFuture.awaitUninterruptibly();

//连接成功后获取会话对象。如果没有上面的等待,由于connect()方法是异步的,

//connectFuture.getSession(),session可能会无法获取。

udpClient.setSession(connectFuture.getSession());

udpClient.getSession().write("Hello,UDPServer!");

}

public IoSession getSession() {

return session;

}

public void setSession(IoSession session) {

this.session = session;

}

public IoConnector getConnector() {

return connector;

}

public void setConnector(IoConnector connector) {

this.connector = connector;

}

public InetSocketAddress getInetSocketAddress() {

return inetSocketAddress;

}

public void setInetSocketAddress(InetSocketAddress inetSocketAddress) {

this.inetSocketAddress = inetSocketAddress;

}

}         2、ClientIoHandler.java

import org.apache.mina.core.service.IoHandlerAdapter;

import org.apache.mina.core.session.IoSession;

/**

* @description

* @date:(2015-11-29 上午10:22:39)

* @author Administrator

* @version v1.0

* @since v1.0

*

* Modified history

*

* Modified date:

* Modifier user:

* description:

*

* */

public class ClientIoHandler extends IoHandlerAdapter{

@Override

public void sessionOpened(IoSession session) throws Exception {

System.out.println("创建session连接");

}

@Override

public void sessionClosed(IoSession iosession) throws Exception {

System.out.println("会话连接关闭");

iosession.close(true);

}

@Override

public void messageReceived(IoSession iosession, Object obj)

throws Exception {

System.out.println("客户端成功接收到消息:"+obj.toString());

}

@Override

public void messageSent(IoSession iosession, Object obj) throws Exception {

System.out.println("客户端消息已发送成功,发送数据:"+obj.toString());

}

}

3、UDPServer.java

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.charset.Charset;

import org.apache.mina.filter.codec.ProtocolCodecFilter;

import org.apache.mina.filter.codec.textline.LineDelimiter;

import org.apache.mina.filter.codec.textline.TextLineCodecFactory;

import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;

/**

* @description

* @date:(2015-11-29 上午10:39:25)

* @author Administrator

* @version v1.0

* @since v1.0

*

* Modified history

*

* Modified date:

* Modifier user:

* description:

*

* */

public class UDPServer {

public static void main(String[] args) throws IOException {

NioDatagramAcceptor acceptor=new NioDatagramAcceptor();

acceptor.getFilterChain().addLast("codec",

new ProtocolCodecFilter(

new TextLineCodecFactory(

Charset.forName("UTF-8"),

LineDelimiter.WINDOWS.getValue(),

LineDelimiter.WINDOWS.getValue())));

acceptor.setHandler(new ServerIoHandler());

acceptor.bind(new InetSocketAddress("127.0.0.1",9897));

}

}

4、ServerIoHandler.java

import org.apache.mina.core.service.IoHandlerAdapter;

import org.apache.mina.core.session.IoSession;

/**

* @description

* @date:(2015-11-29 上午10:23:05)

* @author Administrator

* @version v1.0

* @since v1.0

*

* Modified history

*

* Modified date:

* Modifier user:

* description:

*

* */

public class ServerIoHandler extends IoHandlerAdapter{

@Override

public void sessionOpened(IoSession iosession) throws Exception {

System.out.println("会话连接已建立");

}

@Override

public void messageReceived(IoSession iosession, Object obj)

throws Exception {

System.out.println("Server收到客户端数据:"+obj.toString());

iosession.write("Hello,Client!");

}

@Override

public void messageSent(IoSession iosession, Object obj) throws Exception {

System.out.println("Serve端响应客户端发送数据:"+obj.toString());

}

}运行UDPServer.java,UDPClient.java;运行结果如下:

server端:

会话连接已建立

Server收到客户端数据:Hello,UDPServer!

Serve端响应客户端发送数据:Hello,Client!

Client端:

创建session连接 客户端消息已发送成功,发送数据:Hello,UDPServer! 客户端成功接收到消息:Hello,Client!

最后

以上就是动人御姐最近收集整理的关于java udp mina,用MINA实现UDP通信的例子的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部