I have two standalone exe's Parent.exe and Child.exe.
Parent.exe opens Child.exe and some operation are performed in child.exe by user then
child.exe sends back some processed data to Parent.exe. Both exe's stays on same machine
The communication between Child.Exe and Parent.Exe is being done through TCP IP (Socket Programming). For this we need to calculate the Local IP Address, we used to calculate it in the following way :-
string hostname = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostEntry(hostname);
IPAddress[] addr = ipEntry.AddressList;
string FinalIpAddrs = addr[0]
Everything worked fine at XP but with Windows 7 we are facing problem. The below line return Mac Address what is the right way to do it, so that it works on XP and Win7 both?
string FinalIpAddrs = addr[0]
Oh, those wh program and do not know.
You do NOT need to know an IP Address, use 127.0.0.1, which is a local loopback address. SImple like that - it is there for exactly that reason.
Consider being efficient. If that is all same machine, the usage of TCP is bad. Use named pipes, which are a windows integrated commmunciation mechanism that CAN work cross machine, but on the same machine it uses shared memory which is a LOT more efficient than TCP.
Why not just use IPAddress.Loopback (or IPv6Loopback)?
The loopback addresses are designed for communications entirely on the same host.
I'm also guessing that where you say:
The below line return Mac Address
What you actually mean is that it's returning an IPv6 address. There's no way that that code should be able to return a MAC Address.
Is there a specific requirement NOT to use 127.0.0.1? If no, then I'd just use it without any calculations.
Related
Writing a chat program (as so many do) and i have found that i would like to be able to get the clients to connect to the server automatically.
However, the IP address of the server would not be permanent, so i cannot just hard-core it into the program
In TCP, I'm looking for some sort of broadcast feature, that allows the client to know where the server is
Any ideas?
EDIT: should have said, this will be a LAN program only - no outside connections
If you are talking about a chat in a LAN and you can't or don't want to use DNS for some reason, you could implement, or find an implementation of, the discovery protocol used by UPnP. The SSDP is based on a UDP broadcast. It is, afaik, not possible to multicast via TCP, because TCP needs a session.
If you want to use the chat server over the internet you have no choice but to use DNS. Look for a dynamic dns provider (I use selfhost.bz). In C# you can then resolve the hostname to an IP address as described in the other answers. If you have a hostname to connect to it will probably be enough to pass that to the socket, though:
socket.Connect("myhostname.selfhost.bz", ...
Edit: Since you say you're in a LAN, a few more details on SSDP. The protocol does way more, than you actually need. If you're thinking of implementing it yourself, don't stick to it exactly. Just make your clients send a broadcast on a specified port. The server permanently listens on that port, answering with a predefined message, once it receives a message. When the client receives that answer, it will know that the sender is a valid server.
Use DNS. Resolve the hostname in your app and connect to the IP it resolves to. You'll need dynamic DNS since you say the IP isn't permanent.
Use the below process to find server IP address
public string GetIPAddress()
{
string strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
return ipAddress.ToString();
}
You can also use
Request.ServerVariables["LOCAL_ADDR"];
I had an idea: just get the server to write the IP address/port/whatever to a textfile somewhere on the (public) network, and the clients can read the text file
Obviously, if the text file is not there or empty, no server is running...
Is this such a bad idea?
I'd like the user to be able to enter the MAC of a computer on the network. Once they've done that, it'll add it to a list. The program will then ping all of those MACs on the list every time the class is called (I know this isn't necessarily possible, but read on).
Normally I'd simply use IP addresses, but they aren't static, and there are a -lot- of devices on the network that I don't care about the connectivity of for this program. If they don't respond, I'd like it to pop up a message box.
NOW, that being said, the only part I'm having trouble with is the actual part where I ping something. I know that an actual "ping" is not possible when it comes to MAC addresses, so how could I check for something like that? Alternatively, if it's easier, I could also accept pinging something based on the computer name.
EDIT: I'd prefer not to have to use things like arp to find the IP addresses of the devices I want. Like I said though, I'm also interested in whether or not it's possible to search for devices by name. Would that work?
If you have a properly administered environment, you should be using names. By properly administered environment, I'm primarily meaning having a DNS server on your local network.
I have something similar running that pings industrial ethernet devices. These are statically assigned addresses, so they don't register themselves with DNS as a DHCP client would. I had our DNS administrator create records for them so I can just use their name. You'll be better off in the long run as two years from now you're going to have NO idea what that mac address in your list was referring to. When creating names, you can make them as descriptive as necessary.
EDIT: Here's a function that takes a name as a string, looks up the associated IP from DNS, then pings. If DNS resolution fails or the ping doesn't report success, the function returns false. It returns true otherwise. You should also log the exceptions for troubleshooting later, BTW.
public bool Check(string Name)
{
//try dns resolution, if fails, quit reporting error
IPAddress[] addresses = null;
try
{
addresses = Dns.GetHostAddresses(Name);
}
catch (SocketException)
{
return false;
}
//ping remote address
PingReply reply = ping.Send(addresses[0]);
switch (reply.Status)
{
case IPStatus.Success:
return true;
break;
default:
return false;
break;
}
}
EDIT 2: Here are the namespaces I'm using in this project. Not sure what's where exactly, but adding these three will get everything going.
using System.Net.NetworkInformation;
using System.Net;
using System.Net.Sockets;
You need to use RARP http://en.wikipedia.org/wiki/Reverse_Address_Resolution_Protocol
and
MAC address to IP Address on Same LAN in C#
Depending on the network, you may be able to use the arp cache to look up the IP of a given MAC address (even if your network is set up such that you could do this, it will only work if the MAC address of the other machine is in the cache at the time of the request). You can read about arp on Wikipedia
If you have the MAC address you can get the IP address and machine name using MAC address. Once you have the IP you can simply ping the machine.
See this code for an example of how to get IP address from MAC address
I have a program in C# that I want to get a news feed from a server I setup in my basement. I also want to setup this program so it can work locally. To do this I THINK I need to compare the resolved ip of my dyndns.biz hostname to my router's public ip (I have dynamic ip and a client on my server updating the ip of the hostname) and thus determine if the hostname needs to be used or the local ip of the server (192.168.0.100) or the hostname. I already have code to connect to the ftp server assuming I can get the right usage of the hostname versus the localized IP.
Edit: Anyways, in summary because I realized this might not look like a question, how can I determine a) the resolved IP of the hostname and b) the public IP of my router in a C# app
You're overcomplicating this.
Just open your hosts file (found in C:\Windows\System32\drivers\etc) and add your dyndns hostname routing it to loopback. That way you don't have to add any workaround code to your final application to prevent it from even asking your DNS or router:
127.0.0.1 yourhostname.dyndns.biz
To obtain the IP address of a hostname, use the following code:
IPAddress[] addresses = System.Net.Dns.GetHostAddresses("www.cnn.com");
To obtain your public IP address of your router or local network, you need to talk to an outside system that can tell you that part, and unfortunately I don't know if there is any such system that is free to use as well as easy to use from a program.
What is the easiest way to find the next unused IP address on a local adaptors subnet?
IPAddress GetNextFreeIP(NetworkInterface intface)
{
...?
}
Update
I've written a BOOTP/TFTP server that could be used in many different scenarios (cross-over cable, small private network, large corporate network). I have to give the small embedded system I'm updating an IP address and would like to find a spare one for it to use...
If you have DHCP-managed IP ranges, you have an instance you can talk to - the DHCP server (like David recommended)
If you don't have managed IP ranges and no other instance you can talk to, there is no reliable way to tell if an IP is used or unused. IP does not offer such a service itself.
Apart from asking the DHCP-server, I guess you could do a broadcast ping on the subnet and collect the responses from all computers and simply sort the IP-addresses to find the next one. This assumes, of course, that all devices are on-line and responds to broadcasts. So in a pretty controlled environment, this could work.
The short answer is you can't, as Thorsten79 just said.
A slightly longer answer:
It depends on your network's configuration: how the admin has decided to allocate ip addresses. Usually there's a mix of manually assigned ip addresses for servers, routers etc, and a set of dhcp assigned ip addresses for workstations and such.
If you can talk to the dhcp server you might find out which addresses in the reserved range are free, but for the rest of the addresses you cannot find out.
The more interesting question is what you are trying to accomplish? Perhaps it can be realized in a different manner?
From what I've seen of embedded devices with a TFTP server, they boot up with a hard coded IP Address that is documented. After a set period of time (~3-10 seconds), the boot loader transfers control to the application that reads its configuration and sets the IP Address.
If someone wants to use the boot loader to load new firmware, then they need to read the documentation, make sure that IP Address is reachable (on the same subnet), reboot the device, and TFTP the application to the device.
You are all technically right when you say there is no way to ensure an IP isn't being used. But I've decided to rely on this for now...
public static IPAddress FindNextFree(this IPAddress address)
{
IPAddress workingAddress = address;
Ping pingSender = new Ping();
while (true)
{
byte[] localBytes = workingAddress.GetAddressBytes();
localBytes[3]++;
if (localBytes[3] > 254)
localBytes[3] = 1;
workingAddress = new IPAddress(localBytes);
if (workingAddress.Equals(address))
throw new TimeoutException("Could not find free IP address");
PingReply reply = pingSender.Send(workingAddress, 1000);
if (reply.Status != IPStatus.Success)
{
return workingAddress;
}
}
}
I'm following a tutorial # http://www.geekpedia.com/tutorial239_Csharp-Chat-Part-1---Building-the-Chat-Client.html to try and gather the basics of networking. For those not wanting to hit the jump, it's a quick tut demonstrating how to program a simple client-server-model chat application.
When I try and run the code in the tut, it works fine as long as both the client and the server are on the same network, but the second I try and do it externally (getting a mate to run the client app, and running the server app my side), it all goes to pot. The fact that the code works when in the same network leads me to believe that it's not a coding issue, but an issue with the way my network is set up.
I'm trying to run the server on my IP address at port 21719, which I have opened, but still other people can't connect to my server, not able to get any form of response at all.
The code (from the tut) that is being used for the server to listen to connections is:
public void StartListening()
{
IPAddress ipaLocal = ipAddress; //ipAddress is parsed from txtIP
tlsClient = new TcpListener(ipaLocal, 21719);
tlsClient.Start();
ServRunning = true; //for the running loop
// Start the new tread that hosts the listener
thrListener = new Thread(KeepListening);
thrListener.Start();
}
Now, the tutorial does actually point out that
IPAddress ipaLocal = ipAddress;
Will cause issues on some configurations, and I'm beginning to fear that my configuration may be included in that.
So, does anyone have any solution for me?
Thanks,
Sam
What is the local IP address that you're using? (ipAddress) If it's 127.0.0.1, that's not correct (I don't know how it would work internally either, but Windows seems to use magic from time to time). Also, if you have multiple NICs in your local machine, maybe the port forwarding is only set up to forward to one of them, and you're using the IP of the other?
If that's not the problem, here are a few generic suggestions:
Grab a copy of netcat. It's a small network testing util whose only job is to form a simple TCP connection. That will allow you to eliminate your code as a variable in all this. If netcat can form a connection, then you know the problem is your code. If not, you've confirmed that it's your router.
You can use WireShark (or TShark) to look for ICMP packets. Capture ICMP packets on the remote machine. If you get "Destination Unreachable" from the router, you've again proved that it's your router.
As Spencer said you need to make sure Port Forwarding is setup on your router, to forward all packets that come in on port 21719 to your internal machine. As for exactly how to do that, it's hard to say without knowing what type of router.
Are you having people use your external (internet) IP address? (See yours here.)
Have you pinholed your router to forward all communications from port 21719 to your server?
Some tips:
What kind of operating system are you using? Please check the Scope and/or Profiles (under Advanced tab) of your firewall rule.
While your friend is trying to telnet to the port (connect to the im server) monitor the traffic using Wireshark or Network Monitor (Wireshark have problems with Vista and Win 7). If you don't see anything hitting your machine the problem is probably on the router side. Double check the settings - you said you set the forward rule (NAT) but did it also set the rule on firewall of your router?