这是其在同一台PC,我试图发送一个UDP包,上运行的Android设备模拟器中运行(这是错误?)UDP数据包NPE - Java的
发送代码:
private byte[] buf = new byte[1];
private DatagramPacket p;
private int port = 4442;
private InetAddress ip;
private DatagramSocket s;
try {
ip = InetAddress.getByName(Dataholder.ip);//This one is holding the IP , it is confirmed that it is stored
} catch (UnknownHostException e) {}
try {
s = new DatagramSocket(port , ip);
} catch (SocketException e) {}
private void sendPacket(int num){////NUM IS 1 DIGIT ONLY!
buf = Integer.toString(num).getBytes();
p = new DatagramPacket(buf , buf.length, ip,port);
try {
s.send(p);//Null pointer this line
} catch (IOException e) {}
}
和接收代码:
byte[] data = new byte[1];
s = new DatagramSocket(4442);
p = new DatagramPacket(data, data.length);
while(true){
s.receive(p);
}
我实际使用PC的IPv4地址...
错误究竟发生在哪里? – Smutje
它的评论.... s.send(p); – user3310358