Link to home
Start Free TrialLog in
Avatar of duke_n
duke_n

asked on

IPs

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?
Avatar of craig_capel
craig_capel



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


ASKER CERTIFIED SOLUTION
Avatar of ckaneta
ckaneta

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of duke_n

ASKER

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?
duke, what are you onabout?????????
ckaneta, do you understand this, cos i don't....... i think we answered the question?.....
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.
kind of, sounds like he's dealing with a dynamic IP.

what, specifically, are you trying to accomplish?(if i may ask)
if he was dealing with a dynamic, whats the use of the code to grab it?.... kinda pointless.....

and ICQ?....
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.....?????
Avatar of duke_n

ASKER

I am sure you got good and correct answers.
I'm just kinda tied up to chack them out right now.
maybe tomorrow, Paco.
Avatar of duke_n

ASKER

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)
duke, tell me something, u checked his code, but u never checked mine, may i ask to why???

Craig C.
Avatar of duke_n

ASKER

His/Her is shorter :).

I tried again with chat.
IT WORKED.
Now, whose answer shall I accept?
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....
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
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.
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
Avatar of duke_n

ASKER

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
duke, fine i know now how ignorant you are, and you can go to hell.......


Best Regards...

Craig C.
U posted "Geez, you'd make a nice bunch of actors."

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




 
Avatar of duke_n

ASKER

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.
Avatar of duke_n

ASKER

And your last two comments were slightly unprofessional.
your unprofessional, period.
Avatar of duke_n

ASKER

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.
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. :)