很早就有用户咨询udp,一直告诉他们不要用tio开发udp应用,其实是自己偷懒,不想写文档,今天花5分钟写上这么个文档,也算是方便一下大家。
话不多说,直接上过程
1、所有的showcase都始于pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>tio-udp-showcase</artifactId> <name>${project.artifactId}</name> <parent> <groupId>org.t-io</groupId> <artifactId>tio-parent</artifactId> <version>3.0.1.v20180601-RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.t-io</groupId> <artifactId>tio-core</artifactId> </dependency> <!-- slf4j-logback绑定 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> </dependency> <!-- redirect apache commons logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency> <!-- redirect jdk util logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> </dependency> <!-- redirect log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
2、在服务器端定义一个UdpHandler
package org.tio.showcase.udp.server; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.tio.core.Node; import org.tio.core.udp.UdpPacket; import org.tio.core.udp.intf.UdpHandler; /** * @author tanyaowu */ public class ShowcaseUdpHandler implements UdpHandler { private static Logger log = LoggerFactory.getLogger(ShowcaseUdpHandler.class); public ShowcaseUdpHandler() { } @Override public void handler(UdpPacket udpPacket, DatagramSocket datagramSocket) { byte[] data = udpPacket.getData(); String msg = new String(data); Node remote = udpPacket.getRemote(); log.info("收到来自{}的消息:【{}】", remote, msg); DatagramPacket datagramPacket = new DatagramPacket(data, data.length, new InetSocketAddress(remote.getIp(), remote.getPort())); try { datagramSocket.send(datagramPacket); } catch (Throwable e) { log.error(e.toString(), e); } } }
3、服务器启动类
package org.tio.showcase.udp.server; import java.net.SocketException; import org.tio.core.udp.UdpServer; import org.tio.core.udp.UdpServerConf; /** * @author tanyaowu * */ public class ShowcaseUdpServerStarter { /** * @param args * @throws SocketException */ public static void main(String[] args) throws SocketException { ShowcaseUdpHandler fpmsUdpHandler = new ShowcaseUdpHandler(); UdpServerConf udpServerConf = new UdpServerConf(3000, fpmsUdpHandler, 5000); UdpServer udpServer = new UdpServer(udpServerConf); udpServer.start(); } }
4、写个客户端启动类,顺便发上100万条消息
package org.tio.showcase.udp.client; import org.tio.core.udp.UdpClient; import org.tio.core.udp.UdpClientConf; /** * @author tanyaowu */ public class UdpClientStarter { /** * @param args * @author tanyaowu */ public static void main(String[] args) { UdpClientConf udpClientConf = new UdpClientConf("127.0.0.1", 3000, 5000); UdpClient udpClient = new UdpClient(udpClientConf); udpClient.start(); long start = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { String str = i + "、" + "用tio开发udp,有点意思"; udpClient.send(str.getBytes()); } long end = System.currentTimeMillis(); long iv = end - start; System.out.println("耗时:" + iv + "ms"); } }
5、启动服务器和客户端
运行:org.tio.showcase.udp.server.ShowcaseUdpServerStarter.main(String[])
运行:org.tio.showcase.udp.client.UdpClientStarter.main(String[])
6、看一下日志
... ... 2018-06-04 13:53:05,682 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999962、用tio开发udp,有点意思】 2018-06-04 13:53:05,682 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999963、用tio开发udp,有点意思】 2018-06-04 13:53:05,682 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999964、用tio开发udp,有点意思】 2018-06-04 13:53:05,682 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999965、用tio开发udp,有点意思】 2018-06-04 13:53:05,682 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999966、用tio开发udp,有点意思】 2018-06-04 13:53:05,682 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999967、用tio开发udp,有点意思】 2018-06-04 13:53:05,682 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999968、用tio开发udp,有点意思】 2018-06-04 13:53:05,682 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999969、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999970、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999971、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999972、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999973、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999974、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999975、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999976、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999977、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999978、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999979、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999980、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999981、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999982、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999983、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999984、用tio开发udp,有点意思】 2018-06-04 13:53:05,683 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999985、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999986、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999987、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999988、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999989、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999990、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999991、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999992、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999993、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999994、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999995、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999996、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999997、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999998、用tio开发udp,有点意思】 2018-06-04 13:53:05,684 INFO o.t.s.u.s.ShowcaseUdpHandler[29]: 收到来自127.0.0.1:50837的消息:【999999、用tio开发udp,有点意思】
7、打完收功
顺便说一下这个文档的代码地址:https://gitee.com/tywo45/tio-udp-showcase,欢迎关注,当然别忘了:旧时王谢堂前燕,飞入寻常百姓家,因为这个才是所有的核心!