Jón Trausti Arason
Jón Trausti Arason

Reputation: 4698

How to listen to specific IP address

On my server, under advanced TCP/IP settings I have two IP addresses added. My question is, how can I specify that I want to listen to the first or second IP? Is there a way to obtain all IP addresses on my machine via .NET and select one to listen to?

Any help would be appreciated. I hope my question is clear.

Thanks.

Upvotes: 0

Views: 4360

Answers (2)

Keith Bloom
Keith Bloom

Reputation: 2439

The TCPListener in System.Net.Sockets accepts an IP and a port on construction:

  Int32 port = 13000;
  IPAddress localAddr = IPAddress.Parse("127.0.0.1");

  // TcpListener server = new TcpListener(port);
  server = new TcpListener(localAddr, port);

The full MSDN article is here and also look at the TCPClient

Upvotes: 1

Nicolas Repiquet
Nicolas Repiquet

Reputation: 9255

Dns.GetHostAddresses queried with an empty string returns the local host adresses. You then can bind your server socket on a specific address (see Socket.Bind and IPEndPoint).

Upvotes: 2

Related Questions