无锡网站营销公司简介,最专业网站建设公司首选,哪里有专门做网站的,域名检测工具IP地址#xff1f;端口号#xff1f;主机名#xff1f;什么是Socket?什么是UDP#xff1f;什么是TCP#xff1f;UDP和TCP区别#xff1f;以上问题请自行百度#xff0c;有标准解释#xff0c;此处不再赘述#xff0c;直接上干货#xff01;实例#xff1a;发送端端口号主机名什么是Socket?什么是UDP什么是TCPUDP和TCP区别以上问题请自行百度有标准解释此处不再赘述直接上干货实例发送端public class UDPSend {public static void main(String[] args) throws IOException {//创建发送端Socket对象DatagramSocket ds new DatagramSocket();//创建数据并打包/** DatagramPacket(byte[] buf,int length,InetAddress address,int port);*/String s This first UDP,im comming;byte[] bys s.getBytes();int length bys.length;InetAddress address InetAddress.getByName(127.0.0.1);int port 8888;//打包DatagramPacket dp new DatagramPacket(bys,length,address,port);//发送ds.send(dp);//释放ds.close();}}接收端public class UDPReceive {public static void main(String[] args) throws IOException {//创建接收端Socket对象DatagramSocket ds new DatagramSocket(8888);//接受数据byte[] bys new byte[1024];DatagramPacket dp new DatagramPacket(bys, bys.length) ;//阻塞ds.receive(dp);//解析数据InetAddress address dp.getAddress();//获取接收到的数据byte[] data dp.getData();//获取具体收到的数据长度int length dp.getLength();//发送人System.out.println(sender :address.getHostAddress());//接收到的内容System.out.println(new String(data,0,length));//释放资源ds.close();}}测试先运行Receive 接收端监听然后运行Send 发送端发送数据然后接收端便能监听到数据最后附上API文档说明以免有人看不明白上面的代码