Improve company productivity with a Business Account.Sign Up

x
?
Solved

inet_ntoa

Posted on 1998-12-20
25
Medium Priority
?
1,260 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
ID: 1352264
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
ID: 1352265
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
ID: 1352266
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
LVL 4

Expert Comment

by:dwwang
ID: 1352267
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
ID: 1352268
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
ID: 1352269
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
ID: 1352270
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
ID: 1352271
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
ID: 1352272
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
ID: 1352273
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
ID: 1352274
correct, because StrToInt converts it to a Integer = max 32767  and 20013258 is bigger then this.
0
 
LVL 1

Author Comment

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

Kind regards,
Arijan
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352276
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
ID: 1352277
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 400 total points
ID: 1352278
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
ID: 1352279
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
ID: 1352280
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
ID: 1352281
Thanks,

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

Expert Comment

by:dwwang
ID: 1352282
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
ID: 1352283
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
ID: 1352284
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
ID: 1352285
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
ID: 1352286
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
ID: 1352287
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
ID: 1352288
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

584 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