Can I choose which Ethernet port I use for UDP communication?

I am writing code that will communicate with the network via UDP. From the udp documentation, all I have to do is specify the destination address and port, and I can start sending / receiving data.

I have two separate Ethernet ports (and possibly a USB-to-RJ45 adapter) that I want to communicate with. The documentation does not indicate whether there is a way to choose which NIC should display my data; can I specify? My users are most likely to be connected to the Internet using their primary Ethernet port, and then using my code to communicate with the second port (or USB-RJ45). (These are two separate networks.)

+4
source share
3 answers

You can choose which interface to use with the LocalHost property when calling udp() :

http://www.mathworks.com/help/instrument/localhost.html

So in your case

 u = udp(rhost,rport,'LocalHost','IP_OF_NIC') 

Btw, if you are interested in how to do this: you can find all the properties of an object using propinfo(u) . Then find the documentation for each property that sounds like what you are looking for.

+2
source

I think the interesting question here is: why would you ever want to choose which network adapter you can communicate with?

If I miss this point, your application should only indicate which server (ip and port) to connect to, and the OS TCP / IP routing scheme should select the appropriate network card for use based on the target network.

+4
source

I have not done anything like this in the past, so I speak only from a theoretical point of view. I understand that LocalHost determines how the local machine will connect to an external object.

Here is the link to the documentation: LocalHost

You must configure LocalHost settings before creating a UDP connection. I also worked out the impression that you know the local IP address of the various network interfaces that you have, and that they are static. I am sure there would be a way to get this information dynamically, but I am not familiar with this from my head.

Good luck and hope that will help! Ben

+1
source

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


All Articles