Anyone know how...

O

O-('' Q)

....to translate the following to C#.NET style?

var
i: Integer;
begin
txtMyIP.Lines.Clear;
sHTML := HTTP.Get('http://www.network-tools.com/');
if sHTML = '' then
Exit;
sIpAddr := 'IP unavailable.';
i := Pos('host', sHTML);
if i = 0 then
Exit;
while sHTML <> 'v' do
Inc(i);
if i >= Length(sHTML) then
Exit;
Inc(i, 7);
x := i + 2;
while sHTML[x] <> '"' do
Inc(x);
if x >= Length(sHTML) then
Exit;
sIpAddr := Copy(sHTML, i, x - i);

txtMyIP.Lines.Add(sIPAddr);

This is Delphi code. The HTTP() part is an Indy IdHTTP component. Is
there something similar I can use in VS2005 to do this? It retrieves
your external IP by parsing the results from network-tools.com and
displays it to the user in a text area.

Any nudges in the right direction would be swell. No direct translation
needed unless someone wants to show that they know both languages well.
:)

Many thanks in advance!
 
C

chris martin

...to translate the following to C#.NET style?
var
i: Integer;
begin
txtMyIP.Lines.Clear;
sHTML := HTTP.Get('http://www.network-tools.com/');
if sHTML = '' then
Exit;
sIpAddr := 'IP unavailable.';
i := Pos('host', sHTML);
if i = 0 then
Exit;
while sHTML <> 'v' do
Inc(i);
if i >= Length(sHTML) then
Exit;
Inc(i, 7);
x := i + 2;
while sHTML[x] <> '"' do
Inc(x);
if x >= Length(sHTML) then
Exit;
sIpAddr := Copy(sHTML, i, x - i);
txtMyIP.Lines.Add(sIPAddr);

This is Delphi code. The HTTP() part is an Indy IdHTTP component. Is
there something similar I can use in VS2005 to do this? It retrieves
your external IP by parsing the results from network-tools.com and
displays it to the user in a text area.

Any nudges in the right direction would be swell. No direct
translation needed unless someone wants to show that they know both
languages well. :)

Many thanks in advance!


Just download the Indy Project library from http://www.indyproject.org/ and
reference it in your C# application
 
O

O-('' Q)

I feel a bit silly for missing that. I never thought to even look and
see if Indy was compliant with c# in VS2005. :blush:

Forgive me for asking my questions, hehe.
 
O

O-('' Q)

I guess my only remaining problem is translating the code which parses
the HTML and kicks back the IP address for the user. Anyone have any
hints for doing so?

Thanks again all.
 
O

O-('' Q)

String sIPAddr;
int i;

txtIP.Clear();
Indy.Sockets.HTTP IdHTTP = new Indy.Sockets.HTTP();
String sHTML = IdHTTP.Get("http://www.network-tools.com/");

if ( sHTML.Contains("") )
{
return;
}
sIPAddr = "IP Not Available!";
i = sHTML.IndexOf("host");
if (i == 0)
{
return;
}
while ( sHTML.Equals("v") ) {
i++;
}
if (i >= sHTML.Length)
{
return;
}
i++;
int x = i + 2;
while ( sHTML[x].Equals(" '' ") ) {
x++;
}
if (x >= sHTML.Length)
{
return;
}
//sIPAddr.CopyTo(i, char(sHTML), x-1, 6);
txtIP.Text = sHTML;

This is what I came up with. As you can probably tell, I am seriously
new to this and am probably so far off from what needs to be done that
I am not even on the map any more.

If anyone can tell me where I am going wrong here and help me to
correct it, I would be ever so grateful.

Yes, I am still learning. I come from Delphi where this just works for
me. Hehe... I know, this isn't delphi. :)
 
O

O-('' Q)

Ok, I got this to compile finally, but it doesn't work. As I said, I am
SURE I am way off base here and the pitcher has just thrown me out.

Can anyone lend a hand with this one? I am just trying to translate the
delphi code listed in the original post to c#.net code.

Thanks in advance, anyone and everyone.

-- Kirby
 
O

O-('' Q)

Bleh... this code is bugging the heck outta me.

Thanks anyway for the help you all DID give. I truly appreciate the
efforts.
 
J

Jon Skeet [C# MVP]

O-('' Q) said:
Bleh... this code is bugging the heck outta me.

Thanks anyway for the help you all DID give. I truly appreciate the
efforts.

Any chance you could post a description of what it's meant to do in
English, rather than in Delphi? I suspect not many readers know Delphi
(I don't) and although we could guess what's wanted, describing it in
words may well suggest a better approach.

I mean, it *looks* like it's trying to skip 'v' characters at the start
of the line, skip the next 7 characters, then take everything up to the
next quote. Frankly, that sounds like the job for a regular expression
rather than a translation of the Delphi code. Does that description
sound correct to you? If so, could you include some sample test strings
(input and desired output) so we could try to get a working solution
for you quickly?

Jon
 
S

Steve Barnett

Ok, I'm learning C# too, so this is my best guess at some of the stuff I see
here...

String sHTML = IdHTTP.Get("http://www.network-tools.com/");
if ( sHTML.Contains("") ) Should be: if (sHTML.Length == 0)

This tests for equality to "v" and you're afer non-equality
while ( sHTML.Equals("v") )

Try changing it to (the exclamation mark negates the result):
while ( !HTML.Equals("v") )

You need to use an escape character to look for " characters:
while ( sHTML[x].Equals("\'") )


See if any of that gets you moving along any further. I doubt it's the only
problems you have as I'm only scanning the code quickly. You never know,
though, it might help.

Steve
 
J

Jon Skeet [C# MVP]

Steve said:
Ok, I'm learning C# too, so this is my best guess at some of the stuff I see
here...

I hope you won't mind a few corrections...
String sHTML = IdHTTP.Get("http://www.network-tools.com/");
if ( sHTML.Contains("") ) Should be: if (sHTML.Length == 0)
Correct.

This tests for equality to "v" and you're afer non-equality
while ( sHTML.Equals("v") )


However, the string indexer returns a character, not a string - and
using == will be somewhat more readable:

while (sHTML=='v')
Try changing it to (the exclamation mark negates the result):
while ( !HTML.Equals("v") )


Indeed (with the caveats above)
You need to use an escape character to look for " characters:
while ( sHTML[x].Equals("\'") )

Very nearly - that's not actually valid C#, as \' isn't a valid escape
sequence; I think you meant \" (given your comment).

Jon
 
O

O-('' Q)

Any chance you could post a description of what it's meant to do in
English, rather than in Delphi? I suspect not many readers know Delphi
(I don't) and although we could guess what's wanted, describing it in
words may well suggest a better approach.

I said in my original post that it parses the HTML to extract the IP
address which is displayed. :)
 
J

Jon Skeet [C# MVP]

O-('' Q) said:
I said in my original post that it parses the HTML to extract the IP
address which is displayed. :)

Yes, but I was looking for a bit more detail than that. For instance,
what the format is. I dare say I could go to networktools.com, poke
around and find out - but it would be a lot quicker if you'd just tell
us...

Jon
 
O

O-('' Q)

Yes, but I was looking for a bit more detail than that. For instance,
what the format is. I dare say I could go to networktools.com, poke
around and find out - but it would be a lot quicker if you'd just tell
us...

Uhh... well... hehe, it parses through the HTML, looks for certain
characters and skips them until it finds what it is looking for.

i := Pos('host', sHTML);

Basically means "if you find the word HOST at this position (i), then
make i that value (the position index)".

Not sure, really, how to explain any better than I originally did.
Sorry for any confusion.
 
O

O-('' Q)

Hi Steve.

Thank you very much for the corrections to my attempt at code. Using
the escape character helped me out a bit more as I did not know how to
do that quote inside quotes.

Still, my code is not working as intended. It compiles now and works,
but to a point. It doesn't parse the HTML and kick back the IP address
like the delphi version does.

Still working this one over. Thanks again, everyone, for the help and
constructive replies.
 
S

Steve Barnett

Bugger - must remember to read back what I write in the future. Still, it
was vaguely in the right direction.

Steve

Jon Skeet said:
Steve said:
Ok, I'm learning C# too, so this is my best guess at some of the stuff I
see
here...

I hope you won't mind a few corrections...
String sHTML = IdHTTP.Get("http://www.network-tools.com/");
if ( sHTML.Contains("") ) Should be: if (sHTML.Length == 0)
Correct.

This tests for equality to "v" and you're afer non-equality
while ( sHTML.Equals("v") )


However, the string indexer returns a character, not a string - and
using == will be somewhat more readable:

while (sHTML=='v')
Try changing it to (the exclamation mark negates the result):
while ( !HTML.Equals("v") )


Indeed (with the caveats above)
You need to use an escape character to look for " characters:
while ( sHTML[x].Equals("\'") )

Very nearly - that's not actually valid C#, as \' isn't a valid escape
sequence; I think you meant \" (given your comment).

Jon
 
S

Steve Barnett

Post your code as it exists now and a sample of the kind of data stream
you're looking at... just a small extract from the data stream that
illustrates the data you're looking for and a few bytes around it. It helps
to be able to see the code you're looking at and the kind of data we're
dealing with.

As I said in my earlier post, there are still errors in the code. I was only
trying to provide a starting point,

Also, have you tried stepping through the code? Put a break on the line
after you retrieve the html and step it through - the watch window should
give you some clues as to what is happening and lets you see the data that
the program is seeing - it's not always what you're expecting to see!

Steve
 
O

O-('' Q)

string sHTML;
char[] sIPAddr = new char[256];
int i;

txtIP.Clear();
Indy.Sockets.HTTP IdHTTP = new Indy.Sockets.HTTP();
sHTML = IdHTTP.Get("http://www.network-tools.com/");

if ( sHTML.Length == 0 )
{
return;
}

i = sHTML.IndexOf( "value=" );
if (i == 0)
{
return;
}
while ( sHTML=='v' ) {
i++;
}
if (i >= sHTML.Length)
{
return;
}
i++;
int x = i + 2;
while ( sHTML[x].Equals("\"") ) {
x++;
}
if (x >= sHTML.Length)
{
return;
}
try
{
sHTML.CopyTo(i, sHTML, x - 1, sHTML.Length);
}
catch (Exception except)
{
// Show error
txtIP.Text = except.ToString();
}
txtIP.Text = sHTML.ToString();

That is my current code. What this NEEDS to do is what this program
does:
http://mysite.verizon.net/unclelugzy/MCDIPsentry.exe

Basically, I am trying to port this program to C#.NET in order to help
me learn more about the language.

I hope this helps some. As for returned data... the code above works,
but it just kicks back ALL of the HTML on the site, rather than
disregarding everything except the IP address.
 
J

Jon Skeet [C# MVP]

O-('' Q) said:
Uhh... well... hehe, it parses through the HTML, looks for certain
characters and skips them until it finds what it is looking for.

i := Pos('host', sHTML);

Basically means "if you find the word HOST at this position (i), then
make i that value (the position index)".

Not sure, really, how to explain any better than I originally did.
Sorry for any confusion.

You could explain it better by being more specific than "looks for
certain characters". Heck, my own description of what I *think* it's
doing was more specific than yours!

As I asked for before, please give us example inputs and desired
outputs.

Jon
 

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