代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
UDP程序测试
作者:
/ 发布于
2011/1/14
/
417
<div><span style="color: #0000ff">package</span><span style="color: #000000"> com.tz.simple.udp; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.io.IOException; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.net.DatagramPacket; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.net.DatagramSocket; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.net.InetAddress; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.Date; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.concurrent.ExecutorService; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.concurrent.Executors; </span><span style="color: #0000ff">import</span><span style="color: #000000"> com.tz.uitl.Common; </span><span style="color: #008000">/**</span><span style="color: #008000"> * UDP程序测试<BR> * 时间服务器,采用线程池处理<BR> * * </span><span style="color: #808080">@author</span><span style="color: #008000"> LJZ * </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> UDPTimeServer </span><span style="color: #0000ff">extends</span><span style="color: #000000"> Thread { </span><span style="color: #0000ff">final</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> DAYTIME_PORT </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">7897</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span><span style="color: #000000"> DatagramSocket serverSocket </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">final</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> THREAD_POOL </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">10</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span><span style="color: #000000"> ExecutorService threadPool </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">; </span><span style="color: #0000ff">public</span><span style="color: #000000"> UDPTimeServer() </span><span style="color: #0000ff">throws</span><span style="color: #000000"> IOException { serverSocket </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> DatagramSocket(DAYTIME_PORT); threadPool </span><span style="color: #000000">=</span><span style="color: #000000"> Executors.newFixedThreadPool(THREAD_POOL); System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">时间服务器启动成功,监听端口 [</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> DAYTIME_PORT </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">] 线程池 [</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> THREAD_POOL </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">].</span><span style="color: #000000">"</span><span style="color: #000000">); } </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> run() { </span><span style="color: #0000ff">while</span><span style="color: #000000"> (</span><span style="color: #0000ff">true</span><span style="color: #000000">) { </span><span style="color: #0000ff">try</span><span style="color: #000000"> { </span><span style="color: #0000ff">byte</span><span style="color: #000000"> buffer[] </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> </span><span style="color: #0000ff">byte</span><span style="color: #000000">[</span><span style="color: #000000">256</span><span style="color: #000000">]; DatagramPacket packet </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> DatagramPacket(buffer, buffer.length); serverSocket.receive(packet); System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">收到客户端 [</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> packet.getSocketAddress() </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000"> ] 请求.</span><span style="color: #000000">"</span><span style="color: #000000">); threadPool.execute(</span><span style="color: #0000ff">new</span><span style="color: #000000"> HandlerThread(packet)); } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (Exception e) { } } } </span><span style="color: #008000">/**</span><span style="color: #008000"> * 客户端数据处理线程 * * </span><span style="color: #808080">@author</span><span style="color: #008000"> LJZ * </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> HandlerThread </span><span style="color: #0000ff">extends</span><span style="color: #000000"> Thread { </span><span style="color: #0000ff">private</span><span style="color: #000000"> DatagramPacket packet </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">; </span><span style="color: #0000ff">public</span><span style="color: #000000"> HandlerThread(DatagramPacket p) { packet </span><span style="color: #000000">=</span><span style="color: #000000"> p; } </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> run() { </span><span style="color: #0000ff">try</span><span style="color: #000000"> { String date </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Date().toString(); </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] buffer </span><span style="color: #000000">=</span><span style="color: #000000"> date.getBytes(); InetAddress address </span><span style="color: #000000">=</span><span style="color: #000000"> packet.getAddress(); </span><span style="color: #0000ff">int</span><span style="color: #000000"> port </span><span style="color: #000000">=</span><span style="color: #000000"> packet.getPort(); packet </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> DatagramPacket(buffer, buffer.length, address, port); serverSocket.send(packet); System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">线程 [</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> getName() </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">] 向客户端 [</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> packet.getSocketAddress() </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000"> ] 发送数据 [</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> Common.byte2string(buffer, </span><span style="color: #0000ff">false</span><span style="color: #000000">) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">].</span><span style="color: #000000">"</span><span style="color: #000000">); } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (Exception e) { } </span><span style="color: #0000ff">finally</span><span style="color: #000000"> { packet </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">; } } } </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> main(String args[]) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> IOException { </span><span style="color: #0000ff">new</span><span style="color: #000000"> UDPTimeServer().start(); } } </span> </div>
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3