Communicate between computers through Ethernet cable - c#

I want to connect two computers with an Ethernet cable without the user having to set up a LAN through the Control Panel and then transfer data between two instances of my app - one running on each computer.
As soon as I have a connection with an IP address for each computer, I know I can easily transfer a file by using Sockets, Pipes(?), WCF... But both computers are also connected via wifi to a router, so how can I somehow tell the computer that for a specific IP address - please use the cable? (I might be getting all of this wrong. Please feel free to correct me.)
I can use a UWP app or a .net (Winform/WPF) app. Either technology is fine. But this must be done in code, not by the users setting up a LAN through the Control Panel. Also, IPv6 is fine too.

Each network card has its own IP address, so both of the computers will have IP address for the WIFI network and another IP address for the ad-hoc link.
The addresses for the ad-hoc link can be set to an address in one IP segment, for example, 10.0.0.1 and 10.0.0.2, if the WIFI addresses are in the range of 192.168.X.X.
Your OS will know which card is related to each IP segment.

Start with System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().
Filter out the Type=EtherNet interfaces and make sure they're Up. You may also have to watch out for virtual adapters etc.
From the selected adapter you can get Address Information etc. The IPV4 address should probably be enough to get you started.

Related

Why does Conveyor by Keyoti have multiple Remote URL IP Address

My machine's local IP is 192.168.1.203 but Conveyor shows multiple IP addresses in the Remote URL IP addresses. What does it mean ?
How can a keyoti achieve this ?
How can a machine have more than one IP different from physical IPs ?
P.S.: I read here that Conveyor uses reverse proxy, but didn't get this.
If you open CMD and run
ipconfig
you will see a list of those IP addresses and the network adapters that they belong to.
It's not uncommon to have multiple internal IP addresses like that, and you will probably see that some belong to virtual adapters such as those for VM Ware or Virtual Box for example.
Conveyor actually listens on any IP, and the IP you see in the Remote URL is just one it picked from the available list that should be accessible from other devices on your network. Sometimes it is wrong and the IP won't work from another device, which is why you see that drop down, so you can manually select the correct one.
Hope that answers your question.

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

.Net classes to detect machines connected via cable

I need to connect two machines via cable and do some work with these once they are connected. Are there any .Net classes which would let me detect two machines connected via cable (when I say cable I do not mean computers on same network but machines which are physically connected)?
I did come across some links on stackoverflow but none seem to have been answered.
Hence any links to previous relevant posts are welcome.
There isn't a .NET class that will just give you this but there are probably some techniques you could use, to varying degrees of success:
If you have access to both machines as part of your process, set a pre-defined static IP address on each device, then if you make sure that only the Ethernet port is active on both devices just wait until you can ping your predefined static IP address and you know they're connected.
Something like https://stackoverflow.com/a/12659133/1742551, running packet sniffing code on one machine to detect when activity is happening on the other end of the cable... not sure how you'd then tell what the IP address of the other machine is though and I guess you'd need this in order to do the work you describe.
You could use the TcpListener class to listen for connections on a specific port number, then from the other machine scan all other available IPs on the same subnet as itself for a device which is listening on this port (assuming that you can ensure both machines are on the same subnet). Assuming this connection is the only connection available, and both devices have no static ip address set, Windows will autoconfigure the IP address due to the lack of DHCP server then they'll both be assigned 169.254.x.y ip addresses as part of the 255.255.0.0 subnet, so this should work.
Probably some other option that I haven't thought of yet, but you get the point...

How to handle any request coming to my virtual wifi hotspot in Windows 7

I want to take control over all the requests which arrive to the virtual wifi hotspot.
I opened up a wifi hotspot via Microsoft Virtual WiFi Mini Adaptor through command prompt. I used netsh wlan start hostednetwork. I also put it into static ip mode to overcome with the android stuck at obtaining ip address issue. Everything is working fine.
Some time ago I built a server software with C# which binds to internal ip address of the wifi hotspot. I use it as a file server, remote computer controlling purposes.
Here is core section: If you connect to the hotspot from a device like android phone or pc, open the browser and type http://192.168.137.1:8080/ (hotspot pc ip address) on address bar, you simply connect to it and a home page appears and so on. But I want every client to be redirected to my server.
So regardless of what they put into address bar like google.com, facebook, yahoo, they will be redirected to my server and visit its homepage. This is something like controlling DNS, I guess. Is it possible to take control over every request that way?
I finally figured it out. First of all I need a DHCP server.
Here is one: http://tftpd32.jounin.net/
This also may help: http://www.wikihow.com/Set-up-DHCP-on-a-Local-Area-Network
With this program I can run and configure its DHCP server. I need to set the DNS server address to my own servers address (like 192.168.0.1 which is default gateway). So when clients connect to my virtual hotspot, they will receive this as a DNS server address. Clients will request every domain name from that address (instead of 8.8.8.8 which is Google Public DNS for example). So I also need a DNS server of my own. It must be bound to port 53 (192.168.0.1:53 with UDP protocol, not TCP!!) which is default DNS port number. Main server must be bound to 192.168.0.1:80 with TCP. Notice that it is bound to HTTP port.
If everything goes fine after doing these, my DNS server will give main servers IP address to clients regardless of the domain name IP they requested. Hence they will connect to my server instead of real ones (facebook,youtube etc).
By the way the DHCP server program which I given the url of is working very well for me. It is very simple and efficient. It will eliminate issues like "Android stuck at obtaining IP address". It also provides other servers such as TFTP, SNTP, SysLog and DNS. It also works so fine even without any advanced virtual hotspot program (such as Connectify). You may only call "netsh wlan start hostednetwork" command. I don't intent any promotion here. I'm just trying to help.
If you want redirecting all domain search queries to your ip address you have to control the DNS.
The best way is to operate as a proxy service between your client and the destination, so you can manipulate the query of the client.

Find and assign a unique IP address to a device

I need to assign a unique IP address (local) for a device we will be connecting into our system via. USB. The actually assigning I will do over RS232 (don't ask!) by telling the device what it's IP is.
I would like to somehow find a usable address relative to the local PC in order to tell the device which to use. Is there a way of enumerating or finding a usable address in C#? I don't want to simply use a fixed one in case of possible clashes with other devices.
I've done a search here and there's lots of people wanting to find the current IP, or the IP of an already existing device, but I can't find much about generating a usable, unique one.
Thanks for any assistance you can give me.
Assigning static IP addresses (even if you ping them) is a bad idea.
What happens if the IP address you've assigned is to a device that happens to be offline right now?
Use DHCP if you can, and consider prompting the user if you can not retrieve an IP address from DHCP as to what it should be assigned to.
I am not ware of a native C# library for DHCP client's, but using pinvoke will get you what you're after. Example code (not tested) here, http://www.ianatkinson.net/computing/dhcpcsharp.htm
From http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol
The Dynamic Host Configuration Protocol (DHCP) is a network
configuration protocol for hosts on Internet Protocol (IP) networks.
Computers that are connected to IP networks must be configured before
they can communicate with other hosts. The most essential information
needed is an IP address, and a default route and routing prefix. DHCP
eliminates the manual task by a network administrator. It also
provides a central database of devices that are connected to the
network and eliminates duplicate resource assignments. In addition to
IP addresses, DHCP also provides other configuration information,
particularly the IP addresses of local Domain Name Server (DNS),
network boot servers, or other service hosts. DHCP is used for IPv4 as
well as IPv6. While both versions serve much the same purpose, the
details of the protocol for IPv4 and IPv6 are sufficiently different
that they may be considered separate protocols.[1] Hosts that do not
use DHCP for address configuration may still use it to obtain other
configuration information. Alternatively, IPv6 hosts may use stateless
address autoconfiguration. IPv4 hosts may use link-local addressing to
achieve limited local connectivity.
Ok, not really sophisticated or anything, but why don't you just try to ping the ips, starting by the lowest, and pick the first one which doesn't respond?

Categories