Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a socket's local endpoint

Tags:

c#

.net

sockets

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

like image 667
Alistair Evans Avatar asked Oct 02 '09 10:10

Alistair Evans


People also ask

What is a local EndPoint?

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.

What is host Contoso com?

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).

How do I find the client port number for server socket programming?

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.


1 Answers

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()

like image 129
Frank Bollack Avatar answered Sep 29 '22 01:09

Frank Bollack