I am looking for the simplest way to block a TCP port in .NET under Windows. I thought the following code would do the job, but it doesn't:
var ipAddress = System.Net.Dns.GetHostEntry("localhost").AddressList[0];
var ipLocalEndpoint = new System.Net.IPEndPoint(ipAddress, port);
var tcpListener = new TcpListener(ipLocalEndpoint);
tcpListener.Start();
I don't understand why that doesn't work. Also, my best idea currently to make it happen is to pop up a server that would serve on the port I want to block. That works, but I'd like an easier solution. Could someone help me out please?
I need to block the provided TCP port in a test suite. I don't want to require admin rights in order to be able to run my tests.