用t-io花5分钟写个udp玩玩,顺便发上100万条消息


声明:本文转载自https://my.oschina.net/talenttan/blog/1823774,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

        很早就有用户咨询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,欢迎关注,当然别忘了:旧时王谢堂前燕,飞入寻常百姓家,因为这个才是所有的核心!

本文发表于2018年06月04日 16:00
(c)注:本文转载自https://my.oschina.net/talenttan/blog/1823774,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2130 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1