UDP 多线程 通讯协议
2016-07-21 18:02
791 查看
以下的ip地址填写程式所属服务器的地址、
1、客户端
2 服务端
package cn.okline.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
public class ClientImpl implements Runnable
{
private Random random = new Random();
private String uuid = UUID.randomUUID().toString();
}
1、客户端
package cn.okline.udp; /** * UDP连接客户端 * 项目名称:[test] * 包:[cn.okline.udp] * 文件名称:[UdpClient] * 描述:[一句话描述该文件的作用] * 创建人:[彭小林] * 创建时间:[2016年7月21日 上午10:04:20] * 修改人:[彭小林] * 修改时间:[2016年7月21日 上午10:04:20] * 修改备注:[说明本次修改内容] * 版权所有:深圳市欧乐在线技术发展有限公司 * 版本:[v1.0] */ public class UdpClient { public static void main(String[] args) { try { for (int i = 0; i < 100; i++) { new Thread(new ClientImpl()).start(); } } catch (Exception e) { e.printStackTrace(); } } }
package cn.okline.udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.util.Arrays; import java.util.Random; import java.util.UUID; public class ClientImpl implements Runnable { private Random random = new Random(); private String uuid = UUID.randomUUID().toString(); public void run() { try { init(); byte[] buffer = new byte[1024 * 64]; // 缓冲区 // 发送随机的数据 byte[] btSend = new byte[] { (byte) random.nextInt(127), (byte) random.nextInt(127), (byte) random.nextInt(127) }; DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("192.168.1.40"), 2233); packet.setData(btSend); System.out.println(uuid + ":发送:" + Arrays.toString(btSend)); try { sendDate(packet); } catch (Exception e) { e.printStackTrace(); } receive(packet); byte[] bt = new byte[packet.getLength()]; System.arraycopy(packet.getData(), 0, bt, 0, packet.getLength()); if (null != bt && bt.length > 0) { System.out.println(uuid + ":收到:" + Arrays.toString(bt)); } /*Thread.sleep(1 * 1000);*/ } catch (Exception e) { e.printStackTrace(); } } /** * 接收数据包,该方法会造成线程阻塞 * @return * @throws IOException */ public void receive(DatagramPacket packet) throws Exception { try { datagramSocket.receive(packet); } catch (Exception e) { throw e; } } /** * 发送数据包到指定地点 * @param bt * @throws IOException */ public void sendDate(DatagramPacket packet) { try { datagramSocket.send(packet); } catch (Exception e) { e.printStackTrace(); } } /** * 初始化客户端连接 * @throws SocketException */ public void init() throws SocketException { try { datagramSocket = new DatagramSocket(random.nextInt(9999)); datagramSocket.setSoTimeout(10 * 1000); System.out.println("客户端启动成功"); } catch (Exception e) { datagramSocket = null; System.out.println("客户端启动失败"); e.printStackTrace(); } } private DatagramSocket datagramSocket = null; // 连接对象 }
2 服务端
package cn.okline.udp; /** * UDP连接客户端 * 项目名称:[test] * 包:[cn.okline.udp] * 文件名称:[UdpClient] * 描述:[一句话描述该文件的作用] * 创建人:[彭小林] * 创建时间:[2016年7月21日 上午10:04:20] * 修改人:[彭小林] * 修改时间:[2016年7月21日 上午10:04:20] * 修改备注:[说明本次修改内容] * 版权所有:深圳市欧乐在线技术发展有限公司 * 版本:[v1.0] */ public class UdpClient { public static void main(String[] args) { try { for (int i = 0; i < 100; i++) { new Thread(new ClientImpl()).start(); } } catch (Exception e) { e.printStackTrace(); } } }
package cn.okline.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
public class ClientImpl implements Runnable
{
private Random random = new Random();
private String uuid = UUID.randomUUID().toString();
public void run() { try { init(); byte[] buffer = new byte[1024 * 64]; // 缓冲区 // 发送随机的数据 byte[] btSend = new byte[] { (byte) random.nextInt(127), (byte) random.nextInt(127), (byte) random.nextInt(127) }; DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("192.168.1.40"), 2233); packet.setData(btSend); System.out.println(uuid + ":发送:" + Arrays.toString(btSend)); try { sendDate(packet); } catch (Exception e) { e.printStackTrace(); } receive(packet); byte[] bt = new byte[packet.getLength()]; System.arraycopy(packet.getData(), 0, bt, 0, packet.getLength()); if (null != bt && bt.length > 0) { System.out.println(uuid + ":收到:" + Arrays.toString(bt)); } /*Thread.sleep(1 * 1000);*/ } catch (Exception e) { e.printStackTrace(); } } /** * 接收数据包,该方法会造成线程阻塞 * @return * @throws IOException */ public void receive(DatagramPacket packet) throws Exception { try { datagramSocket.receive(packet); } catch (Exception e) { throw e; } } /** * 发送数据包到指定地点 * @param bt * @throws IOException */ public void sendDate(DatagramPacket packet) { try { datagramSocket.send(packet); } catch (Exception e) { e.printStackTrace(); } } /** * 初始化客户端连接 * @throws SocketException */ public void init() throws SocketException { try { datagramSocket = new DatagramSocket(random.nextInt(9999)); datagramSocket.setSoTimeout(10 * 1000); System.out.println("客户端启动成功"); } catch (Exception e) { datagramSocket = null; System.out.println("客户端启动失败"); e.printStackTrace(); } } private DatagramSocket datagramSocket = null; // 连接对象
}
相关文章推荐
- Java 程序员应会的 Linux 命令
- MySQL crash-safe replication
- 未关闭InputStream 引起的血案
- Android基础之Activity四种启动模式
- java中的 private Logger log=Logger.getLogger(this.getClass());
- c++学习笔记(十一):C++类的访问修饰符
- 数据结构实验之数组一:矩阵转置
- JavaScript的垃圾回收
- shell初步学习
- sql
- CodeForces 597A Divisibility
- Android 接入微信支付
- TCP/IP协议族
- NotificationManager
- 学习OkHttp wiki--Interceptors
- leetcode Set Matrix Zeroes
- 快速编写“专家级”makefile(3.提高编译环境的实用性)
- Windows下php扩展开发c++动态库
- 一个简单、漂亮、功能强大的Android日志程序:logger
- 在Hadoop集群上的Hive配置