.Net classes to detect machines connected via cable - c#

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...

Related

UdpClient only receives dgrams in one instance on same machine [duplicate]

Can I perform a UDP broadcast packets on my machine? I don't have a network, i just have my cheap linux box. I want to have a server broadcasting a packet and two or more clients in the same machine receiving them. Is that possible? What IP do I use?
#gravyface gave me hope but I tried: 1) server sending to 127.255.255.255:54321 and clients listenting to 0.0.0.0:54321. 2) server sending to 127.255.255.255:54321 and clients listening to 127.0.0.1:54321. 3) server sending to 127.255.255.255:54321 and clients listening to 127.255.255.255:54321. None of them worked! :(
OBS: I am using REUSE_ADDR and SO_BROADCAST options.
I am able to confirm that 127.255.255.255 works on Linux (Ubuntu) and it does not work on Mac (Snow Leopard). If you ever find out how to do that on Mac let me know. :)
You can use virtual network adapters with different virtual addresses. For example in Windows you can use Microsoft Loopback or TUN/TAP in Linux.
Another solution would be to create an internal network with a Virtualization software such as VirtualBox but this would require a slight faster machine. If you could only afford(since it's a cheap box) a single Virtual Machine you could enable 1-4 network adapters that would allow you to bind your different UDP clients individually on each one of the virtual adapters of your virtual device.

Communicate between computers through Ethernet cable

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.

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?

why cant I see UDP traffic generated by c# application in wireshark?

I am using the UDPClient class to send and receive messages on my loopback address. The executables are also interacting with each other. But why doesnt the traffic appear in wireshark?
BTW I am running windows inside parallels on OSX and can select only 1 interface that is my intel pro net network card in wireshark.
Here's the key phrase:
on my loopback address
The loopback address is a complete additional interface, not just an address. Wireshark is configured to listen on a specific interface, and I'm guessing the loopback interface is not it.
The loopback traffic is not captured by the Net Packet Filter driver.
One workaround is to send it to the IP address of your Intel Pro NIC. In my experience, this is enough to hit the NPF capture driver and show up in Wireshark. (Well, to be accurate, my experience in that matter doesn't involve a VM so YMMV).
Of course, the listener should be bound to that NIC IP to receive the packets (and not only to localhost).

TcpListener: Listen on every address, including GPRS IP address

We have a simple piece of legacy software with which we need to
communicate using TCP/IP over port 15001. We need to listen on port 15001
for the legacy software to make a connection and then read whatever it sends us.
We have tested this solution accross the internet and it works just fine.
If however we test the same solution across a GPRS TCP/IP network it does not.
All the basics have been checked, we can ping other devices in the GPRS network
and port 15001 is not blocked by any firewall.
So maybe there is something wrong with my TcpListener?
It is initialized like this:
tcpServer = new TcpListener(IPAddress.Any, TCP_PORT);
I'm assuming it listens on every available IPv4 enabled interface on the system,
because I used IPAddress.Any ?
Does anybody have any idea what the difference might be between the two networks? (Even though there shouldn't be any difference) and if there is something I need to change to my TcpListener?
You need to specify the IP address on which you want to listen, instead of IPAddress.Any. See here. When you use IPAddress.Any, it will automatically choose the network interface for you. To listen on a certain interface (in your case, GPRS) you have to use the correct IP in the constructor.
This post has more information on getting the IP address for each nic.
Also, if you're looking to listen on every IP address at once, you'll need a TcpListener for each nic.

Categories