How can I signal sleep flow in Java?

I am writing a UDP client-server pair for a network class, and I got into a problem. This is a rather unorthodox assignment of networks, so a bit of background first:

The goal is to create a server for implementing push-based notifications. The key point here is that the server must contact the client at any address that it has last seen, as well as listen to client management packets. Therefore, I have a thread running on the client that periodically sends UDP packets to the server, which registers their origin when it needs to send a response. This method also breaks up through NAT, as the transmission updates the address translation.

So, here is my dilemma: if I'm not mistaken, NAT displays its own address and the generated port number on it, the address combination of ports. Therefore, in order to successfully test NAT, I need to move all my packets through one port on the client machine. The update flow was simply to listen to the time, push the update package and return to listening.

Then this is where he gets hairy. If the source thread that wants to perform some action wants a port, it must wake up the speaker, which is blocked, waiting for a response.

How can I extract this in Java?

PS: If it turns out that NAT will allow communication on another port, then everything will be amazing.

+3
source share
4 answers

. , .

: " Java": Call interrupt() . , , , . interrupt() sleep() ing wait() InterruptedException, , , .

, read(), , . , DatagramSocket, :

+3
+2

""?

wait() notify().

0

- , . Java, .

You can have one selector watching both the UDP channel and the incoming channel, waking up from activity.

There's an introduction to the selectors halfway down http://java.sun.com/developer/technicalArticles/releases/nio/ . See also AbstractSelector API documents and its interface .

0
source

Source: https://habr.com/ru/post/1794998/


All Articles