UDP协议是一种不可靠的网络协议,它在通信实例的两端各机建立一个Socket,但这两个Socket之间并没有虚拟链路,这两个Socket只是发送、接收数据报的对象,Java提供了DatagramSocket对象作为基于UDP协议的Socket,使用DatagramPacket代表DatagramSocket发送、接收的数据报.
send()发送数据报。
receive()接收数据报。
package com.net.udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class UdpServer { public static final int port=30000; //定义每个数据包的大小为4k private static int DATA_LEN=4096; //定义该服务器使用的DatagramSocket private DatagramSocket socket=null; //定义接收网路数据的字节数组 byte[] inBuff=new byte[DATA_LEN]; //以指定字节数组创建准备接受数据的DatagramPacket对象 private DatagramPacket inPacket=new DatagramPacket(inBuff, inBuff.length); //定义一个用于发送的DatagramPacket对象 private DatagramPacket outPacket; //定义一个字符串数组,服务器发送该数组的元素 String[] books=new String[]{ "美丽说", "绝世武神", "莽荒纪", "帅哥", "美女" }; public void init(){ try { //创建DatagramSocket对象 socket=new DatagramSocket(port); //采用循环接收数据 for(int i=0;i<1000;i++){ socket.receive(inPacket); //判断inPacket.getData()和inBuff是否是同一个数组 System.out.println(inPacket.getData()==inBuff); //将接收到的内容转换成字符串后输出 System.out.println(new String(inBuff,0,inPacket.getLength())); //从字符串数组中取出一个元素作为发送的数据 byte[] sendData=books[i%4].getBytes(); //以指定字节数组作为发送数据、以刚接收到的DatagarmPacket的源SocketAddress作为目标SocketAddress创建DatagramPacket outPacket=new DatagramPacket(sendData, sendData.length, inPacket.getSocketAddress()); //发送数据 socket.send(outPacket); } } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(socket!=null){ socket.close(); } } } public static void main(String[] args) { new UdpServer().init(); } }
package com.net.udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Scanner; public class UdpClient { public static final int DEST_PORT=30000; public static final String DEST_IP="127.0.0.1"; //定义每个数据报的最大发小为4k private static final int DATA_LEN=4096; //定义该客户端使用的DatagramSocket private DatagramSocket socket=null; //定义接收网路数据的字节数组 byte[] inBuff=new byte[DATA_LEN]; //以指定字节数组创建准备接受数据的DatagramPacket对象 private DatagramPacket inPacket=new DatagramPacket(inBuff, inBuff.length); //定义一个用于发送的DatagramPacket对象 private DatagramPacket outPacket=null; public void init(){ try { //创建一个客户端DatagramSocket,使用随机端口 socket=new DatagramSocket(); outPacket=new DatagramPacket(new byte[0], 0,InetAddress.getByName(DEST_IP),DEST_PORT); //创建键盘输入流 Scanner scan=new Scanner(System.in); //不断读取键盘输入 while(scan.hasNextLine()){ byte[] buff=scan.nextLine().getBytes(); //设置发送用的DatagramPacket里的字节数据 outPacket.setData(buff); //发送数据报 socket.send(outPacket); //读取socket的数据,读到的数据放在inPacket所封装字节数组里 socket.receive(inPacket); System.out.println(new String(inBuff,0,inPacket.getLength())); } } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(socket!=null){ socket.close(); } } } public static void main(String[] args) { new UdpClient().init(); } }
先运行UdpServer ,再运行UdpClient