UDP是基于无连接的,不可靠的传输 与TCP/IP相反
UDP实现私聊,发送方式客户端,接受方式服务器
package netUDP_sc; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.net.SocketException; /** * 发送方 * * @author Administrator * */ public class Sender_V1 { public static void main(String[] args) { try { // 创建发送本地数据对象 SocketAddress senders = new InetSocketAddress("127.0.0.1", 9090); // 创建接受数据对象 SocketAddress Receiver = new InetSocketAddress("127.0.0.1", 9091); // 创建发送方 DatagramSocket socket = new DatagramSocket(senders); int i = 0; while (true) { // 发送字符串 byte[] buf = (i + "hello").getBytes(); // 创建要发送包的对象 DatagramPacket datapacket = new DatagramPacket(buf, buf.length, Receiver); socket.send(datapacket); i++; Thread.sleep(1000); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
接受方;
package netUDP_sc; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketAddress; /** * 接受方 * * @author Administrator * */ public class Receiver_V1 { public static void main(String[] args) { try { // 创建接受方 SocketAddress Receiver = new InetSocketAddress("127.0.0.1", 9091); // 创建传递方 DatagramSocket socket = new DatagramSocket(Receiver); // 创建数据缓冲区 byte[] buf = new byte[20]; DatagramPacket packet = new DatagramPacket(buf, buf.length); while (true) { System.out.println("等待接受数据......"); // 接受数据到包中,如果没有数据则会阻塞 socket.receive(packet); // 获取包中的字节 byte[] bs = packet.getData(); // 将字节转化成字符串 String str = new String(bs); // 输出 发送者的ip地址和字符串 System.out.println(packet.getSocketAddress() + " 发来的" + str); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行;
等待接受数据......
/127.0.0.1:9090 发来的0hello
..............
UDP实现组播
2.UDP组播消息
0.0.0.0 255.255.255.255
A类地址 1.0.0.0~126.0.0.0
B类地址128.0.0.0~191.254.0.0
C类地址:192.0.0.0~223.255.255.0
D类地址224.0.0.0~239.255.255.254(组播地址)
利用MulticastSocket类来实现
package netUDP_组播; /** * 发送方 */ import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.MulticastSocket; import javax.swing.plaf.multi.MultiButtonUI; public class SendAll { public static void main(String[] args) { try { //创建ip InetAddress inetAdd = InetAddress.getByName("230.0.0.1"); int i = 0 ; while(true){ //创建发送数据包 byte[] buf =(i+"王佳").getBytes(); DatagramPacket packet = new DatagramPacket(buf, buf.length,inetAdd,9092); //创建组播和端口 MulticastSocket cast = new MulticastSocket(); //组播消息 cast.send(packet); i++; Thread.sleep(500); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package netUDP_组播; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.MulticastSocket; /** * 接收方 * @author Administrator * */ public class AllReceiver { public static void main(String[] args) throws InterruptedException { try { //创建组播端口 MulticastSocket cast = new MulticastSocket(9092); //创建接收方的地址 InetAddress InetAdd = InetAddress.getByName("230.0.0.1"); cast.joinGroup(InetAdd); while(true){ //创建接收数据包 //创建数据缓冲 byte[] buf = new byte[10]; DatagramPacket packet = new DatagramPacket(buf, buf.length); System.out.println("等待接收数据>>>>>>"); cast.receive(packet); System.out.println(new String(packet.getData())); Thread.sleep(500); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
结果;
等待接收数据>>>>>>
0王佳
...........
//分析所有在同一个局域网的客户端都可以收到该消息 ;可以用组播来实现网络监控