c# get IP of a PC which turned network discovery off - c#

I use the Ping class to test every possible address in the network. But PCs with the option: Turrn off network discovery do not respond to ping (windows can not discover them too). Is there any way to get the IP and name of these PCs?

Related

HttpListenerContext.Request.RemoteEndPoint Ipaddress behaviour

I have a piece of code in a service which listens to a port and then logs the IP addresses using the HttpListenerContext.Request.RemoteEndPoint
I have noticed, that in one machine "A" when the remote endpoint is also on the same machine A, HttpListenerContext.Request.RemoteEndPoint returns the IP address of the machine A i.e something like 192.168.20.12
Where as when the same piece of code is run on a different machine B, with the remote endpoint also in the same machine B, the HttpListenerContext.Request.RemoteEndPoint returns 127.0.0.1
Does anybody know why there is a difference ?
For my application, I would like to have the proper IP address(192.168.20.12) and not the loopback address. How can one go about getting this information ?
More info : Machine A is a physical desktop running windows 7. Machine B is an EC2 instance running windows server 2012.
The problem isn't that HttpListenerContext.Request.RemoteEndPoint is returning the wrong IP address... the issue is that whatever is connecting to the port is connecting to the loopback address and your listener is correctly reporting that. Assuming that you actually want to use your listener to monitor connections from a remote machine, this is really an immaterial concern (since you can't connect to the loopback address from a remote machine). Likewise, if a machine connecting to your port has multiple IP addresses, you aren't going to be able to choose which one HttpListenerContext.Request.RemoteEndPoint returns. It should return whichever IP address the remote machine selected to connect to your server.

Why can't I connect to my server using my public IP?

I have a server program and a client program. While developing the program I run the server and the client on the same machine for convenience. The server starts listening to incoming connections using these lines:
var listener = new TcpListener(IPAddress.Any, 7070);
listener.Start();
The client connects to the server using these lines (simplified):
var client = new TcpClient(AddressFamily.InterNetwork);
client.Connect(IPAddress.Loopback, 7070);
I use IPAddress.Loopback because I run the programs on the same machine. But, knowing that the server and the client won't be necessarily run on the same machine in the future, I changed it to my public IP from http://icanhazip.com (IPAddress.Parse(...)). Because of that the client was unable to connect to the server on the same machine with the exception No connection could be made because the target machine actively refused it <my public ip:7070>.
I tried disabling my firewall but it's still not working. Why is the server refusing the connection? Didn't I specifically tell it to listen to all interfaces with IPAddress.Any?
Why does that happen and how do I fix it?
Here's an answer built from my comments on the question, which are hopefully correct:
Your public IP is provided by your ISP and is actually the address of your router. The router does network address translation (NAT) for outgoing connections from computers within your local network. These requests look to the Internet like they're all coming from one IP, and your router sends responses to the right local computer based on an address translation table. This works for outgoing connections but not incoming connections.
If something tries to open a TCP connection from the Internet to your router, the router has no idea what local computer it might be trying to connect to unless you specifically configure it to forward that traffic to a particular computer on your local network. That's where port forwarding comes in. If you haven't configured port forwarding, the router just says, "sorry, I'm not handling incoming requests on port 7070."
Is your development machine behind a router?
Network traffic sent to you via your public IP address reaches your router on a given port via a specific networking protocol. Your router needs to know where to send this traffic internally on your network. Traffic is coming from the Internet to your machine, and your router either cannot or will not forward the traffic to your computer's machine.
You don't notice this in your day-to-day life thanks to the power of Network Address Translation (NAT) and Universal Plug and Play (UPnP). Glossing over some details here, Network Address Translation allows traffic headers to be modified to route traffic from your public IP to your actual machine's IP on the network. When incoming traffic attempts to open a port for connectivity on your network, the router needs to be configured to forward that traffic appropriately. Universal Plug and Play is a protocol supported on many modern routers to allow software and devices to seamlessly route traffic without the need to forward ports.
This leaves you with two options:
For development purposes, access your router and forward the desired port to your development machine
For a more robust application, especially if you're going to be running this on different machines or different networks, consider adding UPnP support to your application while also understanding that UPnP may not be supported or enabled by some users, in which case port forwarding is still necessary.

Connecting Tcp Client from one device to Tcp Server in another device

The Tcp client/server code I'm working with is here: client and server.
As the title goes, I'm running the server code on my computer and I want to connect to it from another device using the client code. My question is, to what ip address does my client code have to connect? I know there are many related posts, but I'm only getting information on what's wrong rather than the solution.
This is a common problem when developing client/server applications. In a typical home network, there are multiple local IP addresses and a single external IP address. All devices communicating with your network from the outside must use the external IP address. However, when writing client/server applications, if you simply input the external IP address as the address to connect to, you'll quickly discover this won't work.
You need to use Port Forwarding. The client and server will be communicating over a specific port, and Port Forwarding is how your router knows which local IP address to send data to when the client is connecting to the external IP address. You want to login to your router settings, navigate to the section regarding Port Forwarding, and specify that communication over the port you're using in your server should be redirected to the local IP address that your server is running on. Exactly how to change these settings on your router depends on which router you're using.
Run the ipconfig command in a Windows Command Prompt on the machine running your server. Obtain the local IPv4 address from the results. This is the address to use when Port Forwarding the port used by your client/server applications. Adjust your router settings accordingly, and then your client should be able to use your external IP address just fine.
To find your external IP address, any website such as http://www.whatsmyip.org/ should work fine.
If you want to avoid all of these problems for now and simply test your application on your home network, then use the local IPv4 address found when running the ipconfig command on the machine your server is running on. Note that this will only work if both the client and server are running on the same network.

Application connects to a different Wi-Fi network but receives same IP address

I’m working on a C# application. I need to know when the application connects to a different Wi-Fi network. The tricky part here is that the application is assigned the same IP address on both Wi-Fi networks:
The application is currently connected to Wi-Fi #1 with IP address 11.22.33.44.
On the PC where the application runs, I disconnect the PC from Wi-Fi #1.
The PC quickly connects to Wi-Fi #2 and is assigned the same IP address (in this example 11.22.33.44).
The application receives the NetworkChange.NetworkAddressChanged event.
The application loops in the network interfaces returned by NetworkInterface.GetAllNetworkInterfaces() in order to detect an IP address change (by checking if the IP address, assigned to a particular NetworkInterface.Id, has changed).
The problem is that the network interface information has not changed (NetworkInterface.OperationalStatus is still UP and the IP address is the same).
The NetworkChange.NetworkAvailabilityChanged event is not received. If the NetworkInterface.OperationalStatus was to DOWN and then back to UP, I could detect a change.
Any idea?
Is it possible to access the list of Wi-Fi network and find out which one the application is connected with?
Thanks!
Provided you are only interested in testing Wi-Fi networks, you could use the SSID to determine which network you are on.
Get SSID of the wireless network I am connected to with C# .Net on Windows Vista

Client cannot connect to server

I'm newbie in WCF, so I really need your help.
I have two programs which uses WCF.
If they are running on one computer and client uses adress net.tcp:\Localhost:8001\MyService to connect to server(which has adress 192.168.1.1 for example) everything is OK.
But when I'm changing adress for client to net.tcp:\192.168.1.1:8001\MyService I'm getting system.timeoutexception because client cannot connect server.
Thanks for help.
You may need to allow external access to the port in the firewall.
As long as you're communication from localhost to localhost, the firewall doesn't come into play, as these requests are handled by the loopback adapter.
But when communicating from localhost to the machines IP address, you're making a network request that's handled by the firewall (even though you're still on the same machine physically).
By the way: For the server side it doesn't matter whether you start the WCF service with endpoint localhost or 192.168.1.1.
EDIT
I wrote that it doesn't matter whether you listen on localhost or 192.168.1.1 - this is only true if you only have one network adapter available.
As soon as there are two or more network cards (for example: Wireless LAN is turned on and you're connected via cable), localhost or 0.0.0.0 will make the service listen on any adapter. Using 192.168.1.1 will make the service listen only for connections on that IP address.
This is important to know especially in cases where the different network adapters become members of different (sub)networks.
For example: One adapter is connected to the 192.168.1 network and the other adapter is connected to the 192.168.2 network. If your service listens on localhost or 0.0.0.0 it will be reachable from both networks. If it only listens on 192.168.1.1, it will not be available for the 192.168.2 network.
Please check "192.168.1.1" is included in your "hosts" file. And give it a try.
The hosts file can be found in "C:\Windows\System32\drivers\etc"

Categories