Question

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

I have two separate Ethernet ports (and potentially a USB-to-RJ45 adapter) that I want to be able to communicate on. The documentation doesn't specify if there's a way to select what NIC card my data should be output on; is it possible to specify? My users will likely be connected to the Internet with their main Ethernet port, then using my code to do communication with the second port (or USB-to-RJ45). (These are two separate networks.)

Was it helpful?

Solution 2

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, in case you're curious how to approach this: You can find all properties of an object with propinfo(u). Then find the documentation for each property which sounds like what you're looking for.

OTHER TIPS

I think the interesting question here is, why you would ever want to choose which NIC to communicate through?

Unless I'm missing the point, your application should only specify which server (ip and port) to connect to, and the TCP/IP routing scheme of the OS should choose the proper NIC to use based on the target network.

I haven't done anything like this in the past so I'm only speaking from a theoretical point of view. It is my understanding that the LocalHost is what determines how the local machine will connect to an external entity.

Here is a link to the documentation: LocalHost

You'll obviuosly have to setup the LocalHost settings prior to creating the UDP connection. I'm also working off of the impression that you know the local IP address of the different network interfaces you have and that they are static. I'm sure there would be a way to get that information dynamically, but I am not familiar with that off the top of my head.

Best of luck and hope that helps! Ben

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top