I am working in unity 2018. In My project I have to find my website address. I have used this code to find the web address.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
public class IpAddressFind : MonoBehaviour {
public string url="https://www.friendslearn.com";
void Start () {
GetIP(url);
}
public static string GetIP(string url) {
url = url.Replace("http://", ""); //remove http://
url = url.Replace("https://", ""); //remove https://
url = url.Substring(0, url.IndexOf("/")); //remove everything after the first /
try {
IPHostEntry hosts = Dns.GetHostEntry(url);
Debug.Log(hosts.AddressList.Length);
Debug.Log(hosts.HostName);
if(hosts.AddressList.Length > 0)
{
Debug.Log(hosts.AddressList[0].ToString());
Debug.Log("Final");
}
} catch {
Debug.Log ("Could not get IP for URL " + url);
}
return null;
}
}
This returns the CloudFlare address.
Actually my website is working in amazon EC2 and I am using nameserver in CloudFlare. When I run the code it returns the CloudFlare web address. Actually my web address is 13.37.202.259. But it returns CloudFlare's IP.
How to find my EC2 address?
EDIT:I am using old code. Is there any new API to find my IP address. In Unity 2018.
Related
I'm trying to build a connection between the HoloLens and XAMMP server.
when i test the app on unity, it works fine but it doesn't work on the device.
Ps: you can access the server when writing the IP address of the server In Microsoft Edge.
Windows defender firewall is disabled
Network capabilities are checked (publishing settings)
XAMPP server is reconfigured to be accessible to all devices
what am I missing here ? how can I make this work ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour
{
public Text playerDisplay;
public Text score;
public Button submit;
public InputField nameField;
public GameObject bb;
public Text Error;
public void CallLogIn()
{
StartCoroutine(Loginplayer());
}
IEnumerator Loginplayer()
{
WWWForm form = new WWWForm();
form.AddField("name", nameField.text);
WWW www = new WWW("http://192.168.1.100/sqlconnect/test.php", form);
yield return www;
if (www.text[0] == '0')
{
DBManager.username = nameField.text;
DBManager.score = int.Parse(www.text.Split('\t')[1]);
playerDisplay.text = "player: " + DBManager.username;
score.text = "score: " + DBManager.score;
bb.SetActive(true);
}
else
{
Error.text = "save failes. Error #" + www.error;
bb.SetActive(false);
}
DBManager.logedOut();
}
}
I encountered this kind of issue as well. I tried to connect the HoloLens to different entities (MySQL DB, OPCUA Server) but it always fails on the HoloLens (working fine in the editor). Searching on google only revealed similar problems from other people, but no solutions.
So my solution is to use the WWW class to receive data from a HTML-Webserver that hosts plain text. This server (I used nodejs and python) executes the query to the database depending on the GET-request it receives from the HoloLens.
What's the best and simplest way to parse an environment variable of the following possible forms: host[:port]
redis
redis:6379
10.0.0.72
10.0.0.72:6379
my.domain.name
my.domain.name:6379
The inputs are never prepended with a protocol.
The end goal is to parse out the host and port in order to create an object of type System.Net.IPEndPoint, possibly after using Dns.GetHostAddresses(host)[0] depending on whether there is an ip or a hostname supplied in the environment variable.
The purpose is helping dotnet core apps inside various docker containers discover each other according to values specified in their environment variables.
I'm hoping to make use of parsing utilities already available in the dotnet core libraries rather than roll my own parser.
new Uri("redis:6379") produces invalid results.
new Uri("https://redis:6379") produces a Uri object with correct host and port properties, but it seems ridiculous to me that it should be necessary to malform all the inputs by prepending "https://" just to get the Uri object to parse it correctly.
new Uri("https://redis") incorrectly sets the Port property as 443 and doesn't indicate that it wasn't supplied in the user input.
If it's possible to skip parsing steps and resolve straight to IPEndPoint with no in-between steps, that would be my preferred answer.
This is what I'm using at the moment, with error handling removed. Have you got something better?
IPEndPoint ParseGateway(string input) {
var parts = input.Split(':');
var host = parts[0];
var port = parts.Length > 1 ? int.Parse(parts[1]) : DEFAULT_GATEWAY_PORT;
var ip = Dns.GetHostAddresses(host)[0];
return new IPEndPoint(ip, port);
}
Use string method SubString as shown below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] inputs = {
"redis",
"redis:6379",
"10.0.0.72",
"10.0.0.72:6379",
"my.domain.name",
"my.domain.name:6379"
};
string address = "";
string port = "";
foreach (string input in inputs)
{
if (input.Contains(":"))
{
address = input.Substring(0, input.IndexOf(":"));
port = input.Substring(input.IndexOf(":") + 1);
}
else
{
address = input;
port = "";
}
Console.WriteLine("Address : '{0}'; Port : '{1}'", address, port);
}
Console.ReadLine();
}
}
}
I have 2 methods in my program to retrieve the IP Address of the Computer.
1st
public string GetIP1()
{
//using System.Net.Sockets;
return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork).ToString();
}
2nd
public string GetIP2()
{
//using System.IO;
String direction = "";
try
{
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
direction = stream.ReadToEnd();
}
//Search for the ip in the html
int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("</body>");
direction = direction.Substring(first, last - first);
}
catch(Exception){ }
return direction;
}
The 1st code returns an IP that looks like 10.xx.xx.x, and the 2nd code returns IP Address such as 121.xx.xx.xx
Why does the output of these two methods differ?
Obviously, you are behind some NAT.
So by running first code you're receiving your internal network address, and second code gives you real (external) IP address from which your network has access to Internet.
That's because second method is just call to external website determining your IP, and that website only can determine real IP address, not internal one.
In the first method, you are getting the IP Address of your internal network, so if you are behind a router, you will get an internal IP address. This is the address you would see if you ran ipconfig /all from a command prompt.
In the second method you are getting your internet (external) IP address.
I have a simple website in ASP.NET where I have loaded a DLL. I have published the site via IIS and I only want to show on the user side his Machine Name, logged in user and IP. I have tried the following:
My DLL:
namespace ClassLibrary1
{
public class Class1
{
public string getInfo()
{
IPAddress[] ips;
ips = Dns.GetHostAddresses(Dns.GetHostName());
string returns = null;
returns = Environment.MachineName + Convert.ToChar(9) + Environment.UserName;
foreach (IPAddress ip in ips)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
returns += Convert.ToChar(9) + ip.ToString();
}
return returns;
}
}
}
And in the website:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClassLibrary1.Class1 cl = new ClassLibrary1.Class1();
Label2.Text = cl.getInfo();
}
}
The output is not what I expected. In my machine, when I access the site i get
MyMachineName Classic .NET AppPool MyIp
And when anyone else opens it, they also get those informations, not their machinename, logged in user and IP.
So my question is how to retrieve their info?
Thanks in advance.
You are pulling the stats from the machine that is serving the website, not the visitor's machine. You should probably take a look at the HttpRequest.ServerVariables NameValueCollection instead.
Some of those "variables", particularly the ones you are interested in, are derived from the headers in each web request from the client. Keep in mind that you aren't actually talking to the client's machine, these are sent to you from the client. Consequently, there's no guarantee that they will be accurate (proxy, etc.), if they're even there at all.
That said, the ones you are probably interested in are:
var ip = HttpRequest.ServerVariables["REMOTE_ADDR"];
var user = HttpRequest.ServerVariables["REMOTE_USER"]; // Windows auth
var user = HttpRequest.ServerVariables["LOGON_USER"]; // Non-Windows auth
var machine = HttpRequest.ServerVariables["REMOTE_HOST"];
Here's the list of variables to pick from.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
how to get my own IP address in C#?
how to get ip address of machine in c#
Hi all, I am currently developing a c# application for windows using WPF. I would like to get the computers external IP address i.e. the internet address not the local computer ip address or the local router address.
Thanks for your help.
Like stated earlier, you need an external web server. An easy call to HTTP GET with the URL "http://checkip.dyndns.org/" will get you a simple text string with your IP.
You need to have a web server sitting somewhere in the cloud so that you can call and that will be able to give you your external IP address.
Looks like this one is free.
[EDIT]
A simple request to here will get you your ip.
This is a way to get any network address (not necessarily the internet ip) as pointed out in the comments:
IPAddress host = IPAddress.None;
foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
{
host = ip;
if (ip.AddressFamily == AddressFamily.InterNetwork)
break;
}
The only way I have found is to do a httpWebRequest to http://www.whatismyip.com/automation/n09230945.asp and parse the results for the ip
You can try connecting to whatismyip.com, as shown in the code below:
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace DreamInCode.Snippets
{
public class IpFinder
{
public static IPAddress GetExternalIp()
{
string whatIsMyIp = "http://whatismyip.com";
string getIpRegex = #"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
Console.Write(we.ToString());
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
IPAddress externalIp = null;
if (m.Success)
{
externalIp = IPAddress.Parse(m.Value);
}
return externalIp;
}
}
}
NOTE: The code comes from this post http://www.dreamincode.net/forums/topic/24692-showing-the-external-ip-address-in-c%23/ .