I need to create a socket which connects to a server process, but must be restricted to using a specified local adapter. The default behaviour is to use whatever it can, but I need to make sure that a given socket only tries (for example), the LAN connection, if both wifi and LAN are available.
I am using C# and .NET 2.0.
Cheers
The LocalEndPoint property gets an EndPoint that contains the local IP address and port number to which your Socket is bound. You must cast this EndPoint to an IPEndPoint before retrieving any information. You can then call the IPEndPoint. Address method to retrieve the local IPAddress, and the IPEndPoint.
This code for the client will connect to that server (supposedly host.contoso.com will be the name of the server in your particular case most likely will be the IP of your computer or the IP of the computer were you are running your server on).
This can be done using a bind() system call specifying a particular port number in a client-side socket. Below is the implementation Server and Client program where a client will be forcefully get assigned a port number.
The Socket.Bind(EndPoint localEP)
method is your friend. Look here at MSDN for the details.
To get all local adapters and their type look at System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With