Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1203
  • Last Modified:

inet_ntoa

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
aluiken
Asked:
aluiken
  • 9
  • 7
  • 7
  • +1
1 Solution
 
BlackManCommented:
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
 
aluikenAuthor Commented:
I am sorry i realy need an example for this function not a zip file =)

Kind regards,
Arijan
0
 
ZifNabCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dwwangCommented:
Hi, just use then winsock unit:

uses .....,winsock;

and then you can use inet_ntoa and inet_addr as usual.

Regards,
Wang
0
 
dwwangCommented:
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
 
aluikenAuthor Commented:
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
 
dwwangCommented:
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
 
aluikenAuthor Commented:
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
 
ZifNabCommented:
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
 
aluikenAuthor Commented:
Yeah i tried StrToInt and Val but i get an error that the string is not an valid integer value.

Arijan
0
 
ZifNabCommented:
correct, because StrToInt converts it to a Integer = max 32767  and 20013258 is bigger then this.
0
 
aluikenAuthor Commented:
So i should ask: how do i convert a value bigger than 32767 into a string ?

Kind regards,
Arijan
0
 
ZifNabCommented:
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
 
aluikenAuthor Commented:
I am sorry, i am using delphi 4. And you talked about a function that can do the job for me ?
0
 
ZifNabCommented:
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
 
dwwangCommented:
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
 
ZifNabCommented:
Ah, a new comment on the list. Yep, I've heard about a new function ... It must be the something like StrToLongInt.
0
 
aluikenAuthor Commented:
Thanks,

I think that this wil solve my problem.
0
 
dwwangCommented:
Also you should input an valis integer for a real ip address than randomly type some numeric characters :-(
0
 
ZifNabCommented:
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
 
dwwangCommented:
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
 
BlackManCommented:
An Integer in D4 is 32-bit, here's a snip from the help..

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

0
 
ZifNabCommented:
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
 
ZifNabCommented:
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
 
dwwangCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 7
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now