- class UdpRece
- {
- public static void main(String[] args) throws Exception
- {
-
- DatagramSocket ds = new DatagramSocket(10000);
-
- Byte[] b = new Byte[10];
- DatagramPacket dp1 = new DatagramPacket(b,b.length);
- //编译时上面这句会报错,说没有符合的构造器。为什么呢?
-
- ds.receive(dp1);
-
- String ip = dp1.getAddress().getHostAddress();
- String data = new String(dp1.getData(),0,dp1.getLength());
- int port = dp1.getPort();
- System.out.println("主机"+ip+":"+port+"说:"+data);
-
- ds.close();
-
- }
- }
复制代码 |
|