您的位置:首页 > 其它

UDP 多线程 通讯协议

2016-07-21 18:02 791 查看
以下的ip地址填写程式所属服务器的地址、

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; // 连接对象


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: