本文实例讲述了Java基于socket服务实现UDP协议的方法。分享给大家供大家参考。具体如下:
示例1:
接收类:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceiveDemo { public static void main(String[] args) throws IOException{ System.out.println("接收端启动…………"); //udpsocket服务,使用DatagramSocket对象 DatagramSocket ds=new DatagramSocket(10002); //使用DatagramPacket将数据封装到该对象中 byte[] buf=new byte[1024]; DatagramPacket dp=new DatagramPacket(buf, buf.length); //通过udp的socket服务将数据包发送出去,通过send方法 ds.receive(dp); //通过数据包的方法解析数据包中的数据,比如,地址、端口、数据内容等 String ip=dp.getAddress().getHostAddress(); //String name=dp.getAddress().getHostName(); int port=dp.getPort(); String text=new String(dp.getData(),0,dp.getLength()); //System.out.println("-----"+ip+"-----"+name+"-----"+port+"-----"+text); System.out.println("-----"+ip+"----------"+port+"-----"+text); //关闭资源 ds.close(); } }发送类:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; public class UDPSendDemo { public static void main(String[] args) throws IOException{ System.out.println("发送端启动…………"); //udpsocket服务,使用DatagramSocket对象 DatagramSocket ds=new DatagramSocket(8888);//监听端口 //将要发送的数据封装到数据包中 String str="udp传输演示,go"; //使用DatagramPacket将数据封装到该对象中 byte[] buf=str.getBytes(); DatagramPacket dp= new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.100"),10002); //通过udp的socket服务将数据包发送出去,通过send方法 ds.send(dp); //关闭资源 ds.close(); } }示例2:
接收类:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceiveDemo2 { public static void main(String[] args) throws IOException{ System.out.println("接收端启动…………"); //udpsocket服务,使用DatagramSocket对象 DatagramSocket ds=new DatagramSocket(10003); while(true){ //使用DatagramPacket将数据封装到该对象中 byte[] buf=new byte[1024]; DatagramPacket dp=new DatagramPacket(buf, buf.length); //通过udp的socket服务将数据包发送出去,通过send方法 ds.receive(dp);//阻塞式的。 //通过数据包的方法解析数据包中的数据,比如,地址、端口、数据内容等 String ip=dp.getAddress().getHostAddress(); //String name=dp.getAddress().getHostName(); int port=dp.getPort(); String text=new String(dp.getData(),0,dp.getLength()); //System.out.println("-----"+ip+"-----"+name+"-----"+port+"-----"+text); System.out.println("-----"+ip+"----------"+port+"-----"+text); } //关闭资源 //ds.close(); } }发送类:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package com.socket.demo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSendDemo2 { public static void main(String[] args) throws IOException{ System.out.println("发送端启动…………"); //udpsocket服务,使用DatagramSocket对象 DatagramSocket ds=new DatagramSocket(9999);//监听端口 //将要发送的数据封装到数据包中 //String str="udp传输演示,go"; BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));//键盘输入 String line=null; //使用DatagramPacket将数据封装到该对象中 while((line=bufr.readLine())!=null){ byte[] buf=line.getBytes();// DatagramPacket dp= new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.100"),10003); //通过udp的socket服务将数据包发送出去,通过send方法 ds.send(dp); if("886".equals(line)){ break; } } //关闭资源 ds.close(); } }运行效果图如下:
接收:

发送:

希望本文所述对大家的java程序设计有所帮助。