java - How to coordinate send and receive with UDF DatagramSockets -
i have integration test needs coordinate 2 datagramsockets, each running in own thread. 1 socket waits read data blocking call receive(). other socket needs call send(), must happen after receive() has blocked, otherwise data lost.
the code little this:
receiver
byte[] buf = new byte[1024]; new datagramsocket(7654).receive(new datagrampacket(buf, buf.length));
sender
new datagramsocket(7654).send( new datagrampacket("hello".getbytes(charset.forname("utf-8")), 5));
i'm loath put thread.sleep() before send() call, although sufficient allow receiver block. there elegant way this?
wait on semaphore before send(). signal unit before receive() call. given network delays, amazed if udp reply arrived before receive() call made , rx socket set up. make sure raising priority of receive thread, (or lowering priority of send thread).
you wait on semaphore after send() , signal after receive(), ensuring send thread not attempt send again until rx done. not sure how detect comms failures, iirc, java semaphore waits not have timeout :((
rgds, martin
Comments
Post a Comment