prob with Dns.Resolve(). Plz help!

H

Hari

Hi,

I've a strange problem with the Dns.Resolve() method. My app
actually needs to use socket connection to connect to a remote server.
Right now the server app is running on my local machine. So my app
needs to connect to the local server. For this, i use Dns.Resolve() by
passing my pc name as the argument. Heres the code
IPHostEntry add=Dns.Resolve("pocketpcpp");
IPAddress ip=add.AddressList[0];

And the problem is the ip is assigned some wrong IP for the first time.
Later on its assigned the right IP. What could be the problem? The IP
that is returned for the first time is some thing vague, like
"7.0.188.150". Where am i getting this IP from? And its not the IP of
the emulator. However the name of the emulator is different from that
of my system name. I invite any sort of suggestions.


- Hari
 
G

Guest

Dns.Resolve is obsolete. You didn't say what ver of CF you are using.

But have you tried:

string host = Dns.GetHostName();
IPHostEntry ipHost = Dns.GetHostEntry(host);
string myIP = ipHost.AddressList[0].ToString();

Regards
Simon.
 
H

Hari

Hi Simon,

Thanks a lot and sorry for the delayed response. I'm using
VS2003, which should have CF1.0 version. I tried with the code that
you've sent, but Dns is not showing the method "GetHostEntry()". I
think the code that you've given is one which works on VS2005,CF2.0. I
can't use the same code with CF1.0. Please suggest an appropriate
solution for 1.0.


Thank You,

Hari Priya
Simon said:
Dns.Resolve is obsolete. You didn't say what ver of CF you are using.

But have you tried:

string host = Dns.GetHostName();
IPHostEntry ipHost = Dns.GetHostEntry(host);
string myIP = ipHost.AddressList[0].ToString();

Regards
Simon.


Hari said:
Hi,

I've a strange problem with the Dns.Resolve() method. My app
actually needs to use socket connection to connect to a remote server.
Right now the server app is running on my local machine. So my app
needs to connect to the local server. For this, i use Dns.Resolve() by
passing my pc name as the argument. Heres the code
IPHostEntry add=Dns.Resolve("pocketpcpp");
IPAddress ip=add.AddressList[0];

And the problem is the ip is assigned some wrong IP for the first time.
Later on its assigned the right IP. What could be the problem? The IP
that is returned for the first time is some thing vague, like
"7.0.188.150". Where am i getting this IP from? And its not the IP of
the emulator. However the name of the emulator is different from that
of my system name. I invite any sort of suggestions.


- Hari
 
P

Paul G. Tobey [eMVP]

I'm confused. You want the IP of the local host. Why not call
Dns.GetHostName(), then pass that to GetHostByName() to get the IP address?
For that matter, I think that GetHostByName( "localhost" ) should work. All
you need is an IP address that will allow connection back to the local host;
you really shouldn't care which IP it is, as long as it works.

Paul T.

Hari said:
Hi Simon,

Thanks a lot and sorry for the delayed response. I'm using
VS2003, which should have CF1.0 version. I tried with the code that
you've sent, but Dns is not showing the method "GetHostEntry()". I
think the code that you've given is one which works on VS2005,CF2.0. I
can't use the same code with CF1.0. Please suggest an appropriate
solution for 1.0.


Thank You,

Hari Priya
Simon said:
Dns.Resolve is obsolete. You didn't say what ver of CF you are using.

But have you tried:

string host = Dns.GetHostName();
IPHostEntry ipHost = Dns.GetHostEntry(host);
string myIP = ipHost.AddressList[0].ToString();

Regards
Simon.


Hari said:
Hi,

I've a strange problem with the Dns.Resolve() method. My app
actually needs to use socket connection to connect to a remote server.
Right now the server app is running on my local machine. So my app
needs to connect to the local server. For this, i use Dns.Resolve() by
passing my pc name as the argument. Heres the code
IPHostEntry add=Dns.Resolve("pocketpcpp");
IPAddress ip=add.AddressList[0];

And the problem is the ip is assigned some wrong IP for the first time.
Later on its assigned the right IP. What could be the problem? The IP
that is returned for the first time is some thing vague, like
"7.0.188.150". Where am i getting this IP from? And its not the IP of
the emulator. However the name of the emulator is different from that
of my system name. I invite any sort of suggestions.


- Hari
 
H

Hari

Hi Paul,

Its true that i'm connecting to the server on my local machine.
But thats only time being. Later i need to connect to the main server
which is a remote machine. So i need a method which can return the
correct ip, given the machine name even for the first time.
Dns.Resolve() and Dns.GetHostByName(), both does'nt work. The problem
is both the methods return some vague ip like 7.0.188.150 which is not
definitely the ip of my PC or PPC. Heres the code.

private void button2_Click(object sender, System.EventArgs e)
{
_socClient = new
System.Net.Sockets.Socket(AddressFamily.InterNetwork,SocketType.Stream
,ProtocolType.Tcp );

/// Get the remote IP address of the machine with the given server
name
// IPHostEntry add=Dns.Resolve("pocketpcpp");
IPHostEntry add = Dns.GetHostByName("localhost");


/// Retreive the first ip from the list of ips, of the given host
IPAddress ip=add.AddressList[0];

int iPortNo = 666;

/// Create the end point (represents the network endpoint of
/// a given ip and port)
IPEndPoint ipEnd = new IPEndPoint (ip,iPortNo);


try
{
/// Connect to the remote host...
_socClient.Connect(ipEnd );

/// Set the connection flag to true
// _isConnected = true;
MessageBox.Show(" Connected");
}
catch(Exception)
{
/// Set the connection flag to false
// _isConnected = false;
MessageBox.Show("Not Connected");
}

}

But plz mind, this happens only the first time, the button is clicked.
So it displays "Not Connected". But from the second click on the button
its returning the ip of my pc. So its working, dispalying "Connected".
But why does it not succeed the first time? Whats the prob. Plz suggest
the right solution. And remember i can't hardcode the ip, because the
server can be any machine on which the server application is
running.Thanks a lot.


- Hari




I'm confused. You want the IP of the local host. Why not call
Dns.GetHostName(), then pass that to GetHostByName() to get the IP address?
For that matter, I think that GetHostByName( "localhost" ) should work. All
you need is an IP address that will allow connection back to the local host;
you really shouldn't care which IP it is, as long as it works.

Paul T.

Hari said:
Hi Simon,

Thanks a lot and sorry for the delayed response. I'm using
VS2003, which should have CF1.0 version. I tried with the code that
you've sent, but Dns is not showing the method "GetHostEntry()". I
think the code that you've given is one which works on VS2005,CF2.0. I
can't use the same code with CF1.0. Please suggest an appropriate
solution for 1.0.


Thank You,

Hari Priya
Simon said:
Dns.Resolve is obsolete. You didn't say what ver of CF you are using.

But have you tried:

string host = Dns.GetHostName();
IPHostEntry ipHost = Dns.GetHostEntry(host);
string myIP = ipHost.AddressList[0].ToString();

Regards
Simon.


:


Hi,

I've a strange problem with the Dns.Resolve() method. My app
actually needs to use socket connection to connect to a remote server.
Right now the server app is running on my local machine. So my app
needs to connect to the local server. For this, i use Dns.Resolve() by
passing my pc name as the argument. Heres the code
IPHostEntry add=Dns.Resolve("pocketpcpp");
IPAddress ip=add.AddressList[0];

And the problem is the ip is assigned some wrong IP for the first time.
Later on its assigned the right IP. What could be the problem? The IP
that is returned for the first time is some thing vague, like
"7.0.188.150". Where am i getting this IP from? And its not the IP of
the emulator. However the name of the emulator is different from that
of my system name. I invite any sort of suggestions.


- Hari
 
P

Paul G. Tobey [eMVP]

And Dns.GetHostByName() does the right thing in the case of getting remote
machine IP addresses, too. If this doesn't work, you have a much
lower-level problem than some problem with .NET CF. If there's no DNS
server that knows how to translate that name into an IP address or if the
DNS server which *does* know is not itself known to your network card, then
you're out of luck and you'll have to find some other way to resolve the
name.

You're saying that GetHostByName( "localhost" ) returns an IP that doesn't
make sense? I don't believe you, sorry. You are never printing what
actually comes back from GetHostByName. I think you're misinterpreting the
result of your code to mean that DNS failed. Just do this in your code:

IPHostEntry host = Dns.GetHostByName( "localhost" );
label1.Text = host.AddressList[0].ToString();

Result?

Paul T.

Hari said:
Hi Paul,

Its true that i'm connecting to the server on my local machine.
But thats only time being. Later i need to connect to the main server
which is a remote machine. So i need a method which can return the
correct ip, given the machine name even for the first time.
Dns.Resolve() and Dns.GetHostByName(), both does'nt work. The problem
is both the methods return some vague ip like 7.0.188.150 which is not
definitely the ip of my PC or PPC. Heres the code.

private void button2_Click(object sender, System.EventArgs e)
{
_socClient = new
System.Net.Sockets.Socket(AddressFamily.InterNetwork,SocketType.Stream
,ProtocolType.Tcp );

/// Get the remote IP address of the machine with the given server
name
// IPHostEntry add=Dns.Resolve("pocketpcpp");
IPHostEntry add = Dns.GetHostByName("localhost");


/// Retreive the first ip from the list of ips, of the given host
IPAddress ip=add.AddressList[0];

int iPortNo = 666;

/// Create the end point (represents the network endpoint of
/// a given ip and port)
IPEndPoint ipEnd = new IPEndPoint (ip,iPortNo);


try
{
/// Connect to the remote host...
_socClient.Connect(ipEnd );

/// Set the connection flag to true
// _isConnected = true;
MessageBox.Show(" Connected");
}
catch(Exception)
{
/// Set the connection flag to false
// _isConnected = false;
MessageBox.Show("Not Connected");
}

}

But plz mind, this happens only the first time, the button is clicked.
So it displays "Not Connected". But from the second click on the button
its returning the ip of my pc. So its working, dispalying "Connected".
But why does it not succeed the first time? Whats the prob. Plz suggest
the right solution. And remember i can't hardcode the ip, because the
server can be any machine on which the server application is
running.Thanks a lot.


- Hari




I'm confused. You want the IP of the local host. Why not call
Dns.GetHostName(), then pass that to GetHostByName() to get the IP
address?
For that matter, I think that GetHostByName( "localhost" ) should work.
All
you need is an IP address that will allow connection back to the local
host;
you really shouldn't care which IP it is, as long as it works.

Paul T.

Hari said:
Hi Simon,

Thanks a lot and sorry for the delayed response. I'm using
VS2003, which should have CF1.0 version. I tried with the code that
you've sent, but Dns is not showing the method "GetHostEntry()". I
think the code that you've given is one which works on VS2005,CF2.0. I
can't use the same code with CF1.0. Please suggest an appropriate
solution for 1.0.


Thank You,

Hari Priya
Simon Hart wrote:

Dns.Resolve is obsolete. You didn't say what ver of CF you are using.

But have you tried:

string host = Dns.GetHostName();
IPHostEntry ipHost = Dns.GetHostEntry(host);
string myIP = ipHost.AddressList[0].ToString();

Regards
Simon.


:


Hi,

I've a strange problem with the Dns.Resolve() method. My app
actually needs to use socket connection to connect to a remote
server.
Right now the server app is running on my local machine. So my app
needs to connect to the local server. For this, i use Dns.Resolve()
by
passing my pc name as the argument. Heres the code
IPHostEntry add=Dns.Resolve("pocketpcpp");
IPAddress ip=add.AddressList[0];

And the problem is the ip is assigned some wrong IP for the first
time.
Later on its assigned the right IP. What could be the problem? The
IP
that is returned for the first time is some thing vague, like
"7.0.188.150". Where am i getting this IP from? And its not the IP
of
the emulator. However the name of the emulator is different from
that
of my system name. I invite any sort of suggestions.


- Hari
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top