本帖最后由 横溢天使 于 2015-9-5 08:04 编辑
还请高手指点一下- /*
- 在自己的机子上测试UDP的收发功能
- */
- import java.net.*;
- class Send
- {
- public static void main(String[] args) throws Exception
- {
-
- DatagramSocket ds=new DatagramSocket(8888);
- byte[] buf="abcdef".getBytes();
- DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.101"),10002);
- ds.send(dp);
- ds.close();
-
- }
- }
- class Receive
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket ds=new DatagramSocket(10002);
- while(true)
- {
- byte[] buf=new byte[1024];
- DatagramPacket dp=new DatagramPacket(buf,buf.length);
- ds.receive(dp);
- String ip=dp.getAddress().getHostAddress();
- String data=new String(dp.getData(),0,dp.getLength());
- System.out.println(ip+"*******"+data);
- }
- }
- }
复制代码 |