A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

这个代码理解之后真的超级简单的 ,

public static void main(String[] args) throws Exception {
                byte[] buf = new byte[1024];  
                DatagramSocket ds = new DatagramSocket(8001);
                DatagramPacket dp = new DatagramPacket(buf, 1024);
                ds.receive(dp);  
                String str = new String(dp.getData(),0, dp.getLength());
                System.out.println(str);
                ds.close();
        }
}
发送端
import java.net.*;
public class Test03 {
        public static void main(String[] args) throws Exception {
                DatagramSocket ds = new DatagramSocket(3000);
                String str = "hello world";
                DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),
                                InetAddress.getByName("localhost"), 8001);
                ds.send(dp);
                ds.close();
        }

1 个回复

倒序浏览
加个多线程
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马