Solved

inet_ntoa

Posted on 1998-12-20
25
1,103 Views
Last Modified: 2010-05-18
Hi,

I need an example of the winsock function inet_ntoa and inet_addr. I prefer if it is in the form:

Function ResolveAddress(S : String) : String;
Begin
// so s should contain eg '172727272727'
// and result should contain '127.0.0.1'
End;

Because my program uses strings for the parameters.

Thanks !
0
Comment
Question by:aluiken
  • 9
  • 7
  • 7
  • +1
25 Comments
 
LVL 7

Expert Comment

by:BlackMan
Comment Utility
Get this unit, http://www.rtfm.be/fpiette/files/wsocket.zip, there are examples for both of them...
Or, if you are doing socket programming, help yourself and get the great ICS library (postcardware with source) at http://www.rtfm.be/fpiette/icsuk.htm
0
 
LVL 1

Author Comment

by:aluiken
Comment Utility
I am sorry i realy need an example for this function not a zip file =)

Kind regards,
Arijan
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Hi aluiken,

opening a zip-file can give you source, not :-)? What was the problem with the comment of Blackman?

here are some samples from F. Piette himself from a list, source code like you've asked :

It's easy using winsock call, or any component which encapsulate the winsock dll. Here is the code from my own TWSocket component (freeware with full source code at http://www.rtfm.be/fpiette/indexuk.htm)

function WSocketGetHostByName(Name : String) : PHostEnt;
 var
  szName : array[0..256] of char;
begin
 if not DllStarted then
  LoadWinsock(WINSOCKET);
 StrPCopy(szName, Name);
 Result := WinSock.GetHostByName(szName);
end;

From winsock.pas, we find that HostEnt is a record:

HostEnt = record
 h_name : PChar; { official name of host }
 h_aliases : ^PChar; { alias list }
 h_addrtype : short; { host address type }
 h_length : short; { length of address }
 h_addr_list : ^PChar; { list of addresses }
end;

PHostEnt = ^HostEnt;
The h_addr_list returns all the IP addresses for the host. You can use it like this:

procedure GetIPList(phe : PHostEnt; ToList : TStrings);
 type
  TaPInAddr = array [0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
 var
  pptr : PaPInAddr;
  I : Integer;
 begin
  pptr := PaPInAddr(Phe^.h_addr_list);
  I := 0;
  while pptr^[I] <> nil do begin
   ToList.Add(StrPas(inet_ntoa(pptr^[I]^)));
   Inc(I);
  end;
end;

TWSocket combine both functions in a procedure DNSLookup which is
asynchronous (it can take some time) which means that you get an event when the name resolution is done.
I suggest you download the complete (free) source code from my website.

You have to use a winsock function: GetHostByAddr. You must include winsock in your uses clasue. Then you can call the function.

Here is an extract from TWSocket component (ICS) source file:

function WSocketGetHostByAddr(Addr : String) : PHostEnt;
 var
  szAddr : array[0..256] of char;
  lAddr : u_long;
 begin
  if not DllStarted then
   LoadWinsock(WINSOCKET);
  StrPCopy(szAddr, Addr);
  lAddr := Inet_addr(szAddr);
  Result := WinSock.GetHostByAddr(PChar(@lAddr), 4, PF_INET);
 end;

LoadWinsock does essentially call insock.WSAStartup to start working with the winsock dll.

As you can see, the function returns a PHostEnt type pointer. THostEnt is a record with a member called h_name which is a PChar to the hostname you are looking for. See winsock.pas to get the definition for the record.

 Hope that helps...

  --
 francois.piette@ping.be

Regards, Zif.
0
 
LVL 4

Expert Comment

by:dwwang
Comment Utility
Hi, just use then winsock unit:

uses .....,winsock;

and then you can use inet_ntoa and inet_addr as usual.

Regards,
Wang
0
 
LVL 4

Expert Comment

by:dwwang
Comment Utility
Here is the declaration in Winsock.pas unit:

function inet_addr(cp: PChar): u_long; stdcall; {PInAddr;}  { TInAddr }
function inet_ntoa(inaddr: TInAddr): PChar; stdcall;

0
 
LVL 1

Author Comment

by:aluiken
Comment Utility
This is far to much information, maybe i asked the question wrong. What i am trying to accomplisch is to convert a "coded" ip adress in the form: 5858558837 in 127.0.0.1 and vica versa. I thought it was possible using the inet_ functions
0
 
LVL 4

Expert Comment

by:dwwang
Comment Utility
but isn't this what you want?

var
   i:longint;
   a:tinaddr;
   s:pchar;
begin
     i:=inet_addr('202.96.49.1');
     label1.caption:=inttostr(i); //result in 20013258

     a.S_addr:=20013258;
     s:=inet_ntoa(a);
     label2.caption:=s; //result in 202.96.49.1
end;

0
 
LVL 1

Author Comment

by:aluiken
Comment Utility
Yes i have got this far, but i want to input to be a string and not a fixed integer. I got some problems converting the string to the integer.

0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
aluiken,

Do you work with D4 or D3?
I believe in D4 there is a function to convert a string to longint and I guess this is your problem... not?

Zif.

0
 
LVL 1

Author Comment

by:aluiken
Comment Utility
Yeah i tried StrToInt and Val but i get an error that the string is not an valid integer value.

Arijan
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
correct, because StrToInt converts it to a Integer = max 32767  and 20013258 is bigger then this.
0
 
LVL 1

Author Comment

by:aluiken
Comment Utility
So i should ask: how do i convert a value bigger than 32767 into a string ?

Kind regards,
Arijan
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
This is a possible solution... not so good, but it is a solution if you don't have D4 :

procedure TForm1.Button1Click(Sender: TObject);
var HighOrder, LowOrder : ShortString;
    Number: longint;
begin
 HighOrder := Copy(Edit1.Text,1,4);
 LowOrder := Copy(Edit1.Text,5,4);
 LongInt := StrToInt(HighOrder)*10000+StrToInt(LowOrder);
end;
0
 
LVL 1

Author Comment

by:aluiken
Comment Utility
I am sorry, i am using delphi 4. And you talked about a function that can do the job for me ?
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 100 total points
Comment Utility
where was my mind?

this is far more a better solution :

procedure TForm1.Button1Click(Sender: TObject);
var Number: LongInt;
begin
 Number := Trunc(StrToFloat(Edit1.Text));
end;

Regards, Zif.
0
 
LVL 4

Expert Comment

by:dwwang
Comment Utility
Hi, my code prompted here is fully workable, you can simply copy it and paste it to a form's button1click. and integer is not limited to 32768, it is -2147483648..2147483647, if you are using 32bit Delphi.

regards,
Wang
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Ah, a new comment on the list. Yep, I've heard about a new function ... It must be the something like StrToLongInt.
0
 
LVL 1

Author Comment

by:aluiken
Comment Utility
Thanks,

I think that this wil solve my problem.
0
 
LVL 4

Expert Comment

by:dwwang
Comment Utility
Also you should input an valis integer for a real ip address than randomly type some numeric characters :-(
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
dwwang, an integer is always limited to -.. 32767
A longInt is -2147483648...

And the result of StrToInt is an Integer type not LongInt.

Or am I that wrong?

Zif.
0
 
LVL 4

Expert Comment

by:dwwang
Comment Utility
I have to say that you can copy my code to have a test, and strtoint will work for 20013258, much larger than 32768.

Regards,
Wang
0
 
LVL 7

Expert Comment

by:BlackMan
Comment Utility
An Integer in D4 is 32-bit, here's a snip from the help..

Type      Range      Format
Integer      –2147483648..2147483647      signed 32-bit

0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Wang,

F***, your right! Absolutely right.... Sorry didn't know this at all... how comes? Are you using D4 or D3?

Maybe we've set some compiler options...

Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
aluiken,

is everything working? Have you tested it already? Seeing that you already accepted it, my guess is yes.

Because seeing this thread going on. I get the feeling that this will not solve your problems.

In D3 :

Type      Range      Format

Integer      -32768..32767      Signed 16-bit
Cardinal      0..65535      Unsigned 16-bit
Integer      -2147483648..2147483647      Signed 32-bit
Cardinal      0..2147483647      Unsigned 32-bit

Zif.
0
 
LVL 4

Expert Comment

by:dwwang
Comment Utility
I'm using D3, where integer is -2147483648..2147483647, maybe some compiler option can affect that, I'm not sure. But as BalckMan said, it's written in Delphi's help

take it easy :-)

Regards,
Wang
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now