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.
Than开发者_StackOverflowks.
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).
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
精彩评论