此课程Time
:
public class Time {
int secondPassed = 0;
Timer mytime = new Timer()
TimerTask mytask = new TimerTask() {
public void run() {
secondPassed++;
if(secondPassed == 3) {
secondPassed = 0;
Udp callUdp = new Udp();
String trnsferRP = callUdp.trnsfrRPckt();
System.out.println(trnsferRP + "exp");
System.out.println("Its 10");
}
else {
System.out.println("Second : " + secondPassed);
}
}
};
public void start(){
mytime.scheduleAtFixedRate(mytask, 1000,1000);
}
public static void main(String[] args) throws Exception {
Time starter = new Time();
starter.start();
}
}
这是课程UDP
:
public class Udp {
public DatagramPacket receivePacket;
public String result;
public void socketp() throws Exception {
DatagramSocket clientSocket = new DatagramSocket(10076);
InetAddress IPAddress = InetAddress.getByName("192.168.100.10");
byte[] sendData = new byte[9];
byte[] receiveData = new byte[9];
String sentence = "VDV2bE1";
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 10076);
clientSocket.send(sendPacket);
this.receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
clientSocket.close();
this.result = new String(receivePacket.getData());
return;
}
public String trnsfrRPckt(){
return this.result;
}
}
因此,我尝试将class Udp
中的变量调用到class Time
,因为我想要打印收到的数据包。目前我的结果是:
second : 1
second : 2
nullexp
its 10
为什么null出来了?应该是分组数据+" exp"
答案 0 :(得分:0)
你从未打过电话
callUdp.socketp();
哪个是写入结果变量的唯一函数。因此,在调用socketp()之前,结果为null。