Solved

IPs

Posted on 2000-04-19
26
241 Views
Last Modified: 2010-04-04
This Q has 2 parts:

1) How to get and IP of PC the program is running on?

2)Knowing an IP of another PC, how can I check whether it's connected to the web?
0
Comment
Question by:duke_n
  • 11
  • 8
  • 3
  • +2
26 Comments
 
LVL 2

Expert Comment

by:craig_capel
ID: 2730240


                    unit Unit1;

                    interface

                    uses
                      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, winsock,
                      StdCtrls;

                       type
                                          TName = array[0..100] of Char;
                                          PName = ^TName;

                    type
                      TForm1 = class(TForm)
                        Button1: TButton;
                        Edit1: TEdit;
                        Edit2: TEdit;
                        procedure Button1Click(Sender: TObject);
                      private
                        { Private declarations }
                      public
                        { Public declarations }
                      end;

                    var
                      Form1: TForm1;
                          sIP, sHost, sErr: string;
                       buffer: array[0..50] of char;

                       function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;


                    implementation

                    {$R *.DFM}

                       function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;
                                        var
                                          HEnt: pHostEnt;
                                          HName: PName;
                                          WSAData: TWSAData;
                                          iCnt: Integer;
                                        begin
                                          Result := False;
                                          if WSAStartup($0101, WSAData) <> 0 then begin
                                            sWSAError := 'WSAStartup error';
                                            Exit;
                                          end;
                                          sHostName := '';
                                          sIPAddr := '';
                                          sWSAError := '';
                                          New(HName);
                                          if GetHostName(HName^, SizeOf(TName)) = 0 then begin
                                            Result := True;
                                            sHostName := StrPas(HName^);
                                            HEnt := GetHostByName(HName^);
                                            for iCnt := 0 to HEnt^.h_length - 1 do
                                              sIPAddr := sIPAddr + IntToStr(Ord(HEnt^.h_addr_list^[iCnt])) + '.';
                                            SetLength(sIPAddr, Length(sIPAddr) - 1);
                                          end
                                          else begin
                                            case WSAGetLastError of
                                              WSAEFAULT        : sWSAError := 'WSAEFault';
                                              WSANOTINITIALISED: sWSAError := 'WSANotInitialised';
                                              WSAENETDOWN      : sWSAError := 'WSAENetDown';
                                              WSAEINPROGRESS   : sWSAError := 'WSAEInProgress';
                                            end;
                                          end;
                                          Dispose(HName);
                                          WSACleanup;
                                        end;

                    procedure TForm1.Button1Click(Sender: TObject);
                     begin
                                          if GetHostIP(sHost, sIP, sErr) then begin
                                            Edit1.Text := sHost;
                                            Edit2.Text := sIP;
                                          end

                    end;

                    end.

Ping a host to see if the connection is alive... or try and connect to a web site, and just capture the exception it makes...

try
    //connect
 except
    //it failed, not connected!
end;

Craig C.
0
 
LVL 1

Expert Comment

by:tomer_engel
ID: 2730273
from your q. it's hard to know if one run-time or deign time--

1)desgin time--use winipcfg.exe on your windows directory.

2)run time
two parts now:

a)add to your application a TPowerSocket or TNMGeneralServer (both come with the delphi)components
initize it,and it has propery :localIp

b)that part is a little complex:
from know i'll talk about server(your computer) and client(the computer you are trying to connect to)
  1)if you are writng the server AND the client it's very easy - 'cause delphi internet components have listening ability--meaning they wait for the server to connect them--it's not hard open the help for TserverSocket,tclientScocket and the Chat example comes with the Delphi)..Delphi\demos\internet\chat--and you'll understand it in no time.
  2)if you are writng only the server:: i don't think youre doing that ,after asking the first part of your ques. ,if you are,, ask again and i'll try to help

bye


0
 
LVL 3

Accepted Solution

by:
ckaneta earned 100 total points
ID: 2731194
uses Winsock;

function GetIP: String;
type
  TaPInAddr = Array[0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe: PHostEnt;
  pptr: PaPInAddr;
  Buffer: Array[0..63] of Char;
  I: Integer;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  Result := '';
  GetHostName(Buffer, SizeOf(Buffer));
  phe := GetHostByName(buffer);
  if phe = nil then Exit;
  pPtr := PaPInAddr(phe^.h_addr_list);
  I := 0;
  while pPtr^[I] <> nil do
   begin
    Result := inet_ntoa(pptr^[I]^);
    Inc(I);
   end;
  WSACleanup;
end;

just another way to go---always good to have options
as far as checking to see if another is connected, just as stated above, you just ping it
0
 
LVL 1

Author Comment

by:duke_n
ID: 2731822
I tried the chat example with my friend, and when he asked me for an  address, I entered his IP I had from ICQ, and it didn't connect, y'dig?
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2731889
duke, what are you onabout?????????
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2731899
ckaneta, do you understand this, cos i don't....... i think we answered the question?.....
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2731904
oh yeah.. Questions..... Look at the top left corner of your screen.... Delphi Programming Question...... I would like to point this out to a few people here at EE.......


Craig C.
0
 
LVL 3

Expert Comment

by:ckaneta
ID: 2731928
kind of, sounds like he's dealing with a dynamic IP.

what, specifically, are you trying to accomplish?(if i may ask)
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2731967
if he was dealing with a dynamic, whats the use of the code to grab it?.... kinda pointless.....

and ICQ?....
0
 
LVL 1

Expert Comment

by:tomer_engel
ID: 2732152
craig and ckaneta i agree with you

hey duke_n what exactly do you want--'cause you got three good (or very good) answers so.....?????
0
 
LVL 1

Author Comment

by:duke_n
ID: 2735243
I am sure you got good and correct answers.
I'm just kinda tied up to chack them out right now.
maybe tomorrow, Paco.
0
 
LVL 1

Author Comment

by:duke_n
ID: 2735392
I freed some time and checked ckaneta's answer. It's good.

Now, about connecting two remote apps - it'll take me some yankin' and bankin'(cuz I have to check it together with my friend)
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2735433
duke, tell me something, u checked his code, but u never checked mine, may i ask to why???

Craig C.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:duke_n
ID: 2735497
His/Her is shorter :).

I tried again with chat.
IT WORKED.
Now, whose answer shall I accept?
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2735755
Well If you think its fair to accept someone elses answer after there is nothing wrong with mine... GO AHEAD....

But, if i ask a question, and yours is the first,  I WILL NOT accept it..... what goes around, comes around.....

Craig C....
0
 
LVL 3

Expert Comment

by:ckaneta
ID: 2735833
As far as I'm concerned
I really don't care much about collecting points. So give em to Craig.
both our solutions pretty much do the same thing in pretty much the same way.
Just glad it helped
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2735969
but really, if you don't want to be fair about this... please do give ckaneta the points, if u however said "In the smallest amount of code" i would have given you the answer in a different form, IE take out the error messages and removed the edit buttons and the rest, i thought it may have helped more, i guess i was wrong......

Go A head do it, As for the points, its worthless, i just like to be fair nothing more.... they could take the point system out i and i would still use it.


Craig C.
0
 
LVL 1

Expert Comment

by:tomer_engel
ID: 2736212
HELLO I SUGGESTED THE CHAT IDEA!!!

i don't care about the points too, but....

hey duke,, don't ask us just give the damm points
0
 
LVL 1

Author Comment

by:duke_n
ID: 2736267
Geez, you'd make a nice bunch of actors.

Anyway, secondary tests showed UDP demo connected better.

As for the points, All of you said you don't need'em, so I give them to anyone I want(bwahahahahaha).

That'd be ckaneta , because I tested his/her piece'o'code and because It worked I never got to test any other ones.

Ciao
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2736295
duke, fine i know now how ignorant you are, and you can go to hell.......


Best Regards...

Craig C.
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2736307
U posted "Geez, you'd make a nice bunch of actors."

I post this "Geez, you do make a good idiot"....




 
0
 
LVL 1

Author Comment

by:duke_n
ID: 2737308
I'm sorry If I insulted ye.
(BTW: Insulted by points or the word "actors"?)

What I meant is that the each of you said that you don't care about points but I should give them to him(an actor's or politician's talent: to say many yet to say nothing).

And I wouldn't make a good idiot.
Tell ya why: I already am. If I weren't, I wouldn't be posting those questions here.
0
 
LVL 1

Author Comment

by:duke_n
ID: 2737332
And your last two comments were slightly unprofessional.
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2737527
your unprofessional, period.
0
 
LVL 1

Author Comment

by:duke_n
ID: 2738747
I am a 15-years-old-kid for god sake!
My profession is learning-and that's what I'm trying to do here.
You, on the other hand, are trying real hard to piss me off.
0
 
LVL 1

Expert Comment

by:Subhuman
ID: 2746865
And that's supposed to get you out of giving points? Craig has given you a working answer, that did what you need and more, yet based on conditions unspecified in the original question that Craig could hardly know about, you give the points to another person who gave a solution that did "pretty much ... the same thing in pretty much the same way." Let alone that fact that *you admit* that you didn't even test Craig's code, just because it was a slightly larger copy/paste job!

I know I'm going to avoid your threads in future; Craig, you have found yourself a supporter. :)
0

Featured Post

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!

Join & Write a Comment

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

11 Experts available now in Live!

Get 1:1 Help Now