How to make Arduino Yun listen to local LAN host and remote host on the Interne?

Hello.

I followed this link Yun REST style calls to control my device in LAN. It works fine.

But in that sketch, clients only listen for incoming connection only from localhost (no one from the external network could connect).

server.listenOnLocalhost ();
server.begin ();

My device also needs to listen to external network (for example, Parse server) to do other jobs.

My questions are:

  1. How to make Arduino Yun board listen to local host and remote host?
  2. If the above can be done, how does Arduino Yun board know the command is from local host or remote host?

Thanks,

Nick

I think you are misunderstanding what listenOnLocalhost() is doing.

When you are using the REST style API, you are implementing a socket server in the sketch. That socket is listening for incoming connections from the localhost, ie: the processor running Linux. When you make a web request of the form arduino.local/[b]arduino[/b]/something, you are connecting from an external host to the web server running on the Linux processor. It processes that request, and sees that the first token after the address is /arduino/ so it opens a connection to the socket server running in the sketch, and passes it everything after the /arduino/ token. As far as the sketch is concerned, the connection is coming from the localhost (the web server task on the local Linux processor.) The sketch processes the request, sends and optionally sends a response back to the local web server. That server then sends the response back to the remote node that made the request.

So, from the Yun's Linux processor point of view, it is accepting connections from remote hosts, and it doesn't care whether those connections are from the local network or a remote network. Those requests get translated and passed to the sketch: from the sketch's point of view, it's accepting requests from the local Linux processor.

The alternative is to use noListenOnLocalhost(), which allows the sketch to directly receive connections from remote hosts. But in that case, it's a simple TCP stream socket and you have to write all of the server protocol yourself - you lose the web protocol processing that is being done by the Linux processor. This is probably not what you want.

If you are currently accepting connections from computers on your local private network, and it is working, you don't have to make any changes on your Yun. If you want it to be able to accept connections directly from hosts outside of your private network, then it is a matter of setting up your network and router to properly forward incoming connections to your Yun. It's strictly a network configuration matter, the details of which are out of the scope of this forum, and no changes are needed on your Yun.

But be aware that there isn't much in the way of security enabled in the Yun by default. If you are exposing your Yun to the Internet, you are also exposing it to attack, and once compromised, it will allow easy access to the rest of your private network. So, not only is the network port forwarding setup your responsibility to set up, ensuring security is also your responsibility.