1 package cn.network.demo01; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6 7 import javax.swing.plaf.synth.SynthStyle; 8 9 public class UDPService10 {11 public static void main(String[] args)throws IOException12 {13 //14 DatagramSocket dSocket = new DatagramSocket(6000);15 16 byte[] data = new byte[1024];17 18 while(true)19 {20 DatagramPacket dPacket = new DatagramPacket(data, data.length);21 22 dSocket.receive(dPacket);23 24 int length = dPacket.getLength();25 26 System.out.println(new String(data,0,length));27 System.out.println(dPacket.getAddress().getHostAddress() +":ip");28 }29 30 31 32 //dSocket.close();33 }34 }