C# Identify Network Connected To - c#

I'm working on an app that will copy files to a server.
I need a way to determine if the client is physically attached to the network (LAN cable or wirelessly attached), or if the client is connecting via some other way.
For example, if my app was running on my laptop at the office I might be plugged in. Later I disconnect and go home and my laptop is then connected to my router and my provider.
I thought of checking the domain name but that just gives me the name of my dev PC. I also considered seeing if a file share is available, but if someone who was NOT really connected created a mapping with the same name then its a false result.
In the end the app will determine how it's connected, and then either do a file copy or FTP the file in.
How can I tell how it's really connected?
Thanks

Related

Cannot connect through tcp remotely

I’m developing an app that connects on port 9050 through the public ip of my server. It works fine on lan but if I’m connected on a different network it fails what can I do to fix this?
first try from Command Prompt this command: telnet ip 9050
if it working means the port 9050 between your pc and the server already opened , otherwise you should check why and where the port is blocked (may be firewall in this case you need to create rule in firewall to pass ).
I don't have any code to go by and I know nothing about your network configuration, but I'll tell you what I generally do when debugging a network-enabled app. First, look at your firewall rules. This will obviously vary depending on what OS you're using but on Windows 10 application-specific permissions are located in Control Panel > System and Security > Windows Defender Firewall > Advanced Settings.
After making sure your app can get through your devices' firewall, check the router settings next. Your packets may not getting by whatever inbuilt security protocols are in place. Since your program works with LAN but not over the Internet, this is a distinct possibility. Also, depending on your network configuration, port forwarding may be necessary; if you're sending data via a public IP address, the router needs some way to know which device on the local network needs to be receiving it. Additionally, if you're trying to connect to your server from the same network you're hosting it on, make sure you use the IP loopback address, which I believe is always 127.0.0.1, or in C# IPAddress.Loopback. Public addresses often won't work when connecting to a local host.
If you're still having problems, I recommend using Wireshark. It's an advanced diagnostics tool that tells you exactly what is being sent and received over your network, and can help pinpoint otherwise hard to spot issues.

C# local network discovery of own agents

I have this piece of software, let's call it an agent, deployed on every machine in a local network. Every once in a while, it needs to pull an update from the server.
That means, each machine in that network connects to my server and downloads the new version which is a few MB.
What i want to achieve:
Once the first machine pulls the update from the server, the other machines should get the update from the one(s) that already got it, in a peer network.
The ideas i have so far:
Use a shared folder in the network, first machine that gets the
update places it into that shared folder. Every machine checks the
shared folder first before going to my server. This works, but i get
problems if sharing is not enabled in windows, firewalls, access
rights, etc.
Each agent opens a socket available to the internal network (probably open a tcp socket on 127.0.0.1 on a random port that all
the agents know about). Before pulling the update, broadcast to the
network that you request an update, and if nobody replies that they
have the update, proceed to the server. Else, get the update from
the agent who replied, since they probably already got the update
from the server or from some other agents. The problems i have here
is that i don't really know how to proceed with the network
discovery, nor the performance impact this could have.
Build a local server app, publish it on some node of the internal network (one of the computers) that all the other agents connect to
for updates. This local server gets the update only the first time,
and it "caches" it, giving it to any agent requesting it. This is my
least favorite option, since it implies a local server. If that one
fails, no other node can get updates. Of course, i can implement
fallback mechanism to go to the real server, but still. And also,
every agent will need to know exactly who the "master" is, and that
would be even harder to manage.
If you guys have any better ideas, or any guides to lead me to, would be much appreciated.
Thank you.
Essentially you
either have to maintain a list of the computers that already have the update and pick one randomly to acquire the update,
or have a broadcast query that all computer with the update respond and pick one.
I would go with the broadcast in the spirit of peer-to-peer.

Tell if a mac address is present on a network using c#

I am trying to tell if a mac address is present on a network using c#
We are trying to make a whos in the office dashboard and we will check to see if the person's cell phone connected via wifi (who we will get the mac address from) is
I am not very strong in networking so I am not sure where to even start.
You can run a windows arp -a command line to find out all the devices that are connected to the current network.
You can then parse out the mac address of each line to figure out who is in the office.
MAC addresses are stripped off at the first router/switch, so they're not going to be useful for determining what is connected, unless you can access the monitoring port on your router/switch and you only have one. If you're on a multi-hop network, it's useless.
edit
OK, I've removed a bunch of stuff I said, now that I understand you're talking about cell phones and wifi connections.
The very easiest way to do this that I can come up with is to set your router's DHCP lease time to something short like 10 minutes, then ask the router "who is connected".
The short least time will auto-renew whenever it expires as long as the client is still there.
Many routers display the current DHCP leases on a management page. You can scrape the page with your app and get a list of all currently active DHCP leases.
You can also get it from /tmp/dnsmasq.leases (on the router) if you're running a router that uses dnsmasq. (the location may change but /tmp is pretty common).
The first solution requires parsing a web page and the second requires getting a plain text file from the router and parsing it.
In any case, the best way to know "who is connected" is to ask the device they're connected to.

Ping client LAN from Azure

Using some code from codeproject, I converted a LAN scanner into an ASP.NET web application. The app attempts to ping all ips in a given user inputted range. The app works fine locally. However, I wanted to make the app online. Using the free AZURE subscription, I put the site online. However, it does not scan the user's LAN. This might be because it is trying to scan the LAN of the server computer my app is deployed on. I would like to know if using ASP.NET, it is possible to scan the CLIENT'S LAN instead of the SERVER'S LAN. Please comment if you require the code for the webpage. The website should be online at http://nkdscan.azurewebsites.net/index . Please comment if any other info is required.
No, this is not possible using a server-based scanner, because that server has no way to access the private IP addresses used by the local network. You can only do this from the inside of a network, which means client-side code of some sort.
The problem is that your local network addresses (192.168.., 10...*, etc) are not accessible from the other side of your router. When you connect to a remote server your router does some address translations to make it look like it's sending the request. If you have lots of computers in the same LAN connecting to the same server it's going to look very much like lots of connections from the same place.
If you want build internet server applications then you need to know the basics of how the internet works, and this is one of the important ones. You don't have to know how things happen at the packet level (although that's interesting too), but some understanding of things like network address translation is essential.

C# and ActiveSync communication / open and read files

I have a device connected to a host computer through cradle usb. Now, I'm just wondering if I could use C# sockets to communicate with the device (ie device sending data, host computer processing it then replying back to the device). How can I accomplish this? by that, what ip address etc etc.. do I have to change so that it would connect cause I have the sockets working on wireless. If not, then is there a way to connect to the device, open and read a file (a text document to be more specific) from the device to my host application.. any ideas?
Thanks! :)
Depending on your target device, when you connect via ActiveSync it likely makes a local RNDIS network connection between teh two devices. You can resolve "ppp_peer" as the partner's network name instead of trying to use a hard-coded IP address (IIRC the IP is different on XP than on Vista).
Be aware that it's not a full connection. TCP packets gets passed through, but things like ICMP do not.
Of course, this just gives you a socket connection, just like if you were to connect between two PCs. It's not going to allow you to do file system operations unless you have an app on the other side listening for commands. If you want that type of thing, Microsoft provides the Remote API (RAPI) interface (wrapped in managed code here)for a lot of basic commands, and it can be extended (with C) to do anything you'd like.

Categories

Resources