UDP协议接收端的基本代码

来源:互联网 发布:科比本赛季数据 编辑:程序博客网 时间:2024/05/16 14:16
public class ReceiveDemo {public static void main(String[] args) throws IOException {// 创建接收端的Socket对象DatagramSocket ds = new DatagramSocket(12345);// 创建一个包裹byte[] bys = new byte[1024];DatagramPacket dp = new DatagramPacket(bys, bys.length);// 接收数据ds.receive(dp);// 解析数据String ip = dp.getAddress().getHostAddress();String s = new String(dp.getData(), 0, dp.getLength());System.out.println("from " + ip + " data is : " + s);// 释放资源ds.close();}}

原创粉丝点击