我正在尝试通过UDP协议与网络设备通信以读取不同的值。设备将值写入端口11104。我只想通过此端口上的UDP连接到设备,并想读取这些值。为此,我写了下面的代码,但它不起作用。 (我检查了IP和端口号是否正确)。
public class GetDeviceResponseClass{
public static void main(String[] args){
int deviceport = 11104;
String deviceipaddress_str ="xxx.xx.xx.xxx";
byte[] rcvdata = new byte[1024];
InetAddress deviceipaddress = InetAddress.getByName(deviceipaddress_str);
DatagramSocket rcvsocket = new DatagramSocket();
rcvsocket.connect(deviceipaddress,deviceport);
DatagramPacket rcvpacket = new DatagramPacket(rcvdata,rcvdata.length);
rcvsocket.receive(rcvpacket);//here it is getting stuck
String response = new String(rcvdata,0,rcvpacket.getLength());
System.out.println("response = "+response);
rcvsocket.close();
}
}