Improve company productivity with a Business Account.Sign Up

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

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?
0
duke_n
Asked:
duke_n
  • 11
  • 8
  • 3
  • +2
1 Solution
 
craig_capelCommented:


                    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
 
tomer_engelCommented:
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
 
ckanetaCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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

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

and ICQ?....
0
 
tomer_engelCommented:
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
 
duke_nAuthor Commented:
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
 
duke_nAuthor Commented:
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
 
craig_capelCommented:
duke, tell me something, u checked his code, but u never checked mine, may i ask to why???

Craig C.
0
 
duke_nAuthor Commented:
His/Her is shorter :).

I tried again with chat.
IT WORKED.
Now, whose answer shall I accept?
0
 
craig_capelCommented:
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
 
ckanetaCommented:
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
 
craig_capelCommented:
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
 
tomer_engelCommented:
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
 
duke_nAuthor Commented:
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
 
craig_capelCommented:
duke, fine i know now how ignorant you are, and you can go to hell.......


Best Regards...

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

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




 
0
 
duke_nAuthor Commented:
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
 
duke_nAuthor Commented:
And your last two comments were slightly unprofessional.
0
 
craig_capelCommented:
your unprofessional, period.
0
 
duke_nAuthor Commented:
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
 
SubhumanCommented:
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
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

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.

  • 11
  • 8
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now