I am trying to design a system where there is a Sender and a Receiver and both sides can send and receive packets. I am using 2 datagramchannels for each sender and receiver and 2threads on each side to handle that.
Sending data from sender to receiver works fine, but when I receive that data on the receiver side I want to send an acknowledgement back however, I can't seem to receive on my sender side. Does my logic look correct?
Heres a breakdown of what my classes look like:
Thread class
Thread t = new Thread(new Sender())
Thread t1 = new Thread(new Receiver())
t.start()
t1.start();
Sender class
Sender implements Runnable{
//open channel
//connect to address
//send over data in run method
}
Sender ACK class{
//open channel
//connect
//retrieve and get data received (WAITS For incoming data but data has already been sent)
}
Receiver Class
Receiver implements Runnable{
//open channel
//bind
//Print out data received
//send ack
}
Receiver ACK class{
//open channel
//connect
//send ack method
//send out data
}
I think you need some kind of synchronization to be sure that your receiver is ready to receive message. For example, you can use CountDownLatch
In you Sender you should await() and Receiver should call countDown() on latch object. Try this out to be sure that your Receiver is ready to receive message when the message is sent. If this won't help then show your Sender and Receiver code.