I am making a simple server/client application on windows 8 with winforms. The program works fine until I use my external IP address 50.140.66.104 I get the error.
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll Additional information: The requested address is not valid in its context
I am using the following code:
TcpListener listener = new TcpListener(IPAddress.Parse("50.149.66.104"), 8000);
Why is this IP address an issue? Thanks in advance!!
I suspect that the IP address 50.149.66.104
is your Internet-facing address. The only IP addresses which would be legal to bind to on your PC are addresses assigned to the adapter(s) on that PC. You can't bind to someone else's address (e.g. your router's Internet address).
As for security, there should be no real concern here. All that IPAddress.Any
does is allow the socket to listen on all adapters present on the PC. If for some reason you want to block inbound access to your server on one or more adapters, you can use a firewall (Windows' built-in, or third-party) to block connections on a specific adapter.
Note that Windows also will maintain separate policies for specific networks. Even the same adapter can be configured to allow inbound connections only e.g. when the adapter is connected to a known-safe network, vs. a "Public" network.
If you really want to bind the socket at the server level to a specific network adapter in the machine, you will need to find the IP address that adapter's been set to, and bind to that. Not your public IP address.