I was trying to connect to a server application I built. However, the annoying part is that the host machine of the server application have a dynamic IP. So, I googled around and found out that I can use Dns.GetHostAddresses. But the problem is Dns.GetHostAddresses actually gives all the target machine's IP Adress. Recently I used Dns.GetHostAddresses("Server_Host_Name")[0], which gives me the 1st IP address.
The method have worked so far. But I will like to know is there any safer way of doing this? or this method is fine?
Thanks for any reply and answer.
Related
This is very straightforward. I have a WebSocketServer project that works great if the uri is http://localhost:8081/ but if I use the local IP instead of localhost http://10.0.0.201:8081/, it refuses to answer requests on that IP. I am sure its a permissions issue or unable to access the port issue but I am, frankly, not getting any errors. It just simply doesn't respond. Can anyone shed some light on this? This is all in c#.
Again this is a SERVER. It is supposed to LISTEN on 10.0.0.201 (and, of course, localhost). Localhost works, but the IP does not from any client.
I am intentionally not posting code (although I could if necessary) because this is a multi-threaded application that supports multiple concurrent clients. And it works flawlessly through localhost. I am only changing the word "localhost" to the machine's IP. The failure feels like a binding or permissions issue.
This is probably due to Http Binding in your webserver (e.g. IIS), the binding could be explicitly requiring localhost and if the URI does not have localhost then it would refuse this connection.
Try changing the binding to empty hostname and URI should be allowed.
This is a suggestion, not an answer. Have you tried hitting the WebServer from a different machine? Even if it's just a static test page?
It could be a perms problem, but this would at least tell you if any machine on the local network can get to it using the IP/Port configuration.
Try binding to *
If using HttpListener try this:
httpListener = new HttpListener();
httpListener.Prefixes.Add("http://*/");
instead of
httpListener.Prefixes.Add("http://localhost/");
httpListener.Prefixes.Add("http://10.0.0.201/");
Binding to localhost will not bind to all the rest, and binding to a specific IP address will not work with localhost or IPv6 for example.
Im sorry if this been asked before, but couldnt find anything about this particular matter.
I try to find out with which of my own ip's my computer use to connect to a remote ip.
I use some kind of socket setup both ways etc, and im sending my ip (and other stuff with xml) to another server so it knows how to contact me.
But how do i figure out which ip i should send to it?
I have the servers ip or host name, but trying with
Dns.GetHostEntry("host").AddressList
But that only gives me the ip of the remote host and not how my computer reach it.
Is my question solveable at all or is this just wishful thinking?
Can you have the remote host capture that data? That end would surely have it.
If you're using UDP then this is exactly what the STUN protocol was designed for. STUN is used in VOIP applications (among other P2P systems) to be able to tell what a specific connection looks like on the internet.
One very reliable .NET implementation that implements STUN is included in the Lumisoft.NET library (source code available here). I've used it myself for to satisfy this specific task for applications ranging from VOIP to P2P VPN alternatives. It is very easy to use and is standards compliant.
NOTE: I am NOT in any way affiliated with Lumisoft, I've merely used their library in several different applications
Ok, quick update that wont help anyone except myself.
But.
Found out that i can send hostname instead of actual ip, will work for now and most cases, and let the DNS do what its suppose to do :)
I am facing some unusual problems migrating my application to work in IPv6 environments.
I'm using .NET remoting to connect to a remote service supplying a hostname. That hostname (ipv6.dt112979.company) is registered on my DNS server (running on a Windows 2003 Server with IPv6 enabled in the network card and in the DNS) as an AAAA host.
The problem is that everytime I try to connect to that server a get a "No such host is known.". I've setup Wireshark and no DNS query is being issued. I've done a "ipconfig /flushdns" and the same result is observed.
So, I've created a simple application which simply does:
Dns.GetHostAddresses("ipv6.dt112979.company");
And the result is always "No such host is found".
I've done a NSLOOKUP and the result is:
nslookup ipv6.dt112979.company
Server: company-dev03.company
Address: 10.200.10.207
Name: ipv6.dt112979.company
Address: fe80::a5c3:bfde:5ece:e833
Do you have any idea of what the problem might be?
I've tested this with .NET 3.5 and .NET 4.0 and the problem is the same.
Tests were made in a Windows 7 machine.
If I had a A record for the ipv6.dt112979.company then the call suceeds but only returns the IPv4 address.
Best regards,
Nuno Centeio
I figured out the problem.
I was not in a fully enabled IPv6 network. I was relying on private addresses (FE80::) and, despite of being able to perform ping and nslookup, looks like Winsocks2 doesn't like that.
I've configured Cisco UBE to have a real IPv6 network and everything started working as expected.
Thanks for your help.
Dns.GetHostAddresses returns an array of IPaddress-values, are you sure you are looping through all of them?
Also, if the local computer doesn't support IPv6, the IPv6-results are filtered from the list of results when using Dns.GetHostAddresses. You mentioned that you've ensured that the remote host supports IPv6, but are you sure your local host supports this as well?
1 - How does it work? This question has come up because I am working with a guy for whom the call to System.Net.Dns.GetHostAddresses does not work. I gave him a sample app that just calls the aforementioned method and displays the IP addresses that are returned. Here is his explanation after using it:
When I try to resolve www.google.com [in the app]
it does not
work on the local workstations,
because our internal DNS do not
resolve external DNS names. [But] If I put
www.google.com in the browser, the
proxy server knows to go to the
external DNS servers for name
resolution and it resolves the name to
the IP address. Even if we put the
proxy server name in the application
it does not work when testing the
resolution.
I have done some packet sniffing on my work computer (which has a proxy server) and my personal computer (which does NOT have a proxy server) and there seem to be no packets sent or received durring the GetHostAddresses call. So, what exactly is that call doing?
2:
What port number is used?
Also asked by the person I am working with. I have no idea about this one. If there are no packets sent then it seems the answer would be there is no port being used, but I am not very savvy when it comes to ports and things like this. If anyone knows how to answer this or even a process for me to find the answer, please let me know.
DNS is a huge subject. Concerning Q1, it depends on how your machine is configured (node type). Googling on this should help you on the general principles.
Here is an answer about the ports: Network Ports Used by DNS
The reason in browser works and in command prompt does not is that browser has been setup to use a proxy and command prompt is not.
In order to see what code is used, use reflector. I have been told off to post any Microsoft code but I looked at the code and it was mainly unsafe and Win32 API calls.
The reason you're not seeing any network traffic when you use the packet sniffer could be that google.com is already in your local client DNS cache. If your browser has resolved it, it'll be in the cache. Try ipconfig /flushdns from the command line, and then use nslookup again to resolve google.com. I think you'll see some network traffic then.
Is it possible to write C# code as below and send email using mnetwork in different country?
MSExchangeWebServiceURL = mail.something.com/ews/exchange.asmx
It's a web service URL. This works great when I run the same code from home network, my friends home network anywhere around, but when I run it from my client's location in Columbia it fails.
I have a valid user name and password on that Exchange Server. Is there any configuration that I can set to achieve this?
BTW this code below works when I run it within office network and any network within any home network. I have tried it at least with five friends network in Plano, Texas. I want this code to work running from any network in another country.
My client in Columbia can connect to the web service using a browser using the same user name and password, but when I run the code above it is not able to connect to our web service.
EDIT: Based on the comments and updated post, I would wager that the client location has a proxy server or firewall that is blocking the traffic. The client probably needs to add the relevant domain to whatever "whitelist" they have.
If it works from an internal network, then the problem is most likely not the code. You need to debug it and see what error turns up when running it outside of the network. Then check the logs on the firewall and the target exchange server to see what is happening.
One possible culprit is that the DNS name is not setup outside the office network, or that the office firewall is not setup to pass traffic (or only to pass SMTP traffic) from the internet to the server in question. Another is that Exchange Web Services is not setup properly to accept requests from the outside world. That can be tested from http://www.testexchangeconnectivity.com/.
If this isn't enough for you to get there, post more info about what errors you are seeing. "It isn't working, I want it to work" kind of reminds me of the Star Trek TNG episode about the species who kept telling Geordi to "make it go".
If you are asking if the code works (which it doesn't look like you are but it's in your post) you should ask on stackoverflow. If you are wondering why it won't work from home Its likely because the edge server is not set up to relay, and if you are at home and not vpn'd into the network that's all you have access to.
You need to break this down a bit:
What's the MSExchangeWebServiceURL? I don't need to know your real URL, but is this resolvable from your home network? i.e. is it an internal DNS host (server.mycompany.local) or is it an externally-resolvable DNS host (mail.yourcompany.com)?
If it's internal, like another poster said, you'll at least need to VPN in (or replace that with the external URL).
I'm not a big C# guy, but I'm guessing it's looking for your Exchange Server hostname or FQDN, and since the type is "SMTP" it wants to establish a TCP session with your mail server. You can likely test this out by opening up your command prompt and trying to telnet <MSExchangeWebServiceURL> 25. When that works, my guess is your code will work too.