Solved

inet_ntoa

Posted on 1998-12-20
25
1,165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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 100 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

624 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