- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- public class UDPReceive {
- public static void main(String[] args) {
- try {
- DatagramSocket ds = new DatagramSocket(10010);
- while(true){
- byte[] buff = new byte[2];
- DatagramPacket dp = new DatagramPacket(buff, buff.length);
- ds.receive(dp);
- String ip = dp.getAddress().getHostName();
- String content = new String(buff , 0 , dp.getLength());
- System.out.println(ip+":"+content);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- }
复制代码 如何才能接受可变的byte数组的
也就是说如何接受大小不固定的数据
|
|