UDP编程 .getData()这个方法没有 是为什么 import java.net.*;public class UDPReceDemo {
/**
* 接收数据
* 1、创建udp Sock对象,建立端点
* 2、定义数据包、存储数据
* 3、通过receive()方法接收数据包
* 4、通过数据包方法,解析数据
* 5、关闭资源
* @throws Exception
*/
public static void main(String[] args) throws Exception {
DatagramSocket ds=new DatagramSocket(10000);
while(true){
byte [] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);//阻塞式方法
int port=ds.getPort();
String ip=ds.getLocalAddress().getAddress().toString();
String data= new String(ds.getData,0,buf.length);
System.out.println(port+","+ip);
}
//ds.close();
}
}
|
|