Link to home
Start Free TrialLog in
Avatar of kiranramesh
kiranramesh

asked on

How can I get IP Addressess and machine names of all the machines in LAN

To Broad cast a message from server to all the machines in LAN I have used Socket component in delphi. But it requires machine name or ip address or both. So, to build that into an array I need to know at that particular point of time how many machines are up and what are their respective ip's and/or name ???
Can any one help me with this ???

Thankx in anticipation.
With warm regards,
Kiran.
Avatar of Akazukin
Akazukin

uses
  Winsock;

{...}

function getIPs: Tstrings;
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 := TstringList.Create;
  Result.Clear;
  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.Add(inet_ntoa(pptr^[I]^));
    Inc(I);
  end;
  WSACleanup;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines := GetIps;
end;

Check out the API function NetServerEnum();

HTH
DragonSlayer
listening...
Avatar of kiranramesh

ASKER

Hi Akazukin,

I tried out your code which returns only one ip that's of my machine in which I am executing the code.

Let me explain you the situation.

1.  I have 5 work groups.
2.  And in each of the group there are atleast 5 machines.
3.  In my work group there are 5 machines.

I need all the machines in LAN which are up.
It's very urgent can ne one please help me ???

Kiran.
Hi DragonSlayer,

I tryied out the API function NetServerEnum();
I don't know how to execute that I read the help fro that, but there is no example for that also.

Could you please help me by giving the syntax and execution procedure that helpful.

Regards,
Kiran.
you need a socket component that supports broadcast.

if you have for example a class c network, eg 192.168.0.X,
then the netmask is 255.255.255.0, same for the broadcast mask. If you set the broadcast mask to 255.255.255.128, then you will only broadcast to the upper half of the network for example. The whole point of broadcast is you don't need to know who is listening, maybe no-one is. Like a radio station will still broadcast, even if every radio receiver is turned off, and they don't know/care who is 'out there'

Mark
Hi mullet_attack,

That is precisely why I want to know that how many machines are switched on.
If the machines are switched on and logged on my exe containing the Server socket will be up then what ever message I send across through the client socket only those machine will receive it.

The problem now is I don't know whether the machine is on or not I am sending the message and the error Asynchronous socket error 10060, Which I am trying to avoid. So, I have a point there.

P.S. You cannot use socket component to broadcast by using netmask. You can talk to only one machine at a time.

Regards,
Kiran.
I'm missing something here, i think...

is this right : ?

you have a PC, with your exe running, that exposes a server socket. Other machines (clients) connect to this server. you want to send a message from the server to all the client machines that are currently connected.

BTW, which socket component/s are you using?

Mark
ASKER CERTIFIED SOLUTION
Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia image

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
and here's the source for the form

object Form1: TForm1
  Left = 192
  Top = 103
  Width = 338
  Height = 258
  Caption = 'Server Enum'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 126
    Top = 16
    Width = 24
    Height = 13
    Caption = 'Total'
  end
  object Label2: TLabel
    Left = 126
    Top = 32
    Width = 26
    Height = 13
    Caption = 'Read'
  end
  object lblTotal: TLabel
    Left = 166
    Top = 16
    Width = 6
    Height = 13
    Caption = '0'
  end
  object lblRead: TLabel
    Left = 166
    Top = 32
    Width = 6
    Height = 13
    Caption = '0'
  end
  object Button1: TButton
    Left = 14
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Refresh'
    TabOrder = 0
    OnClick = Button1Click
  end
  object ListView1: TListView
    Left = 14
    Top = 64
    Width = 301
    Height = 150
    Columns = <
      item
        Caption = 'Name'
        Width = 130
      end
      item
        Caption = 'Comments'
        Width = 150
      end>
    ReadOnly = True
    TabOrder = 1
    ViewStyle = vsReport
  end
end
Oh, for the NetServerEnum function to work you need to download the Lan Manager translation from the Project JEDI site... get it from http://www.delphi-jedi.org/Jedi:APILIBRARY:41609



HTH
DragonSlayer.
This program uses one ClientSocket,Label,Button,EditBox components . Type the name of Computer in EditBox and program will show you IP Address of this Computer in Label .

unit UnitIP;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ScktComp, WinSock;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}
function HostToIP(Name: string; var Ip: string): Boolean;
var
  wsdata : TWSAData;
  hostName : array [0..255] of char;
  hostEnt : PHostEnt;
  addr : PChar;
begin
  WSAStartup ($0101, wsdata);
  try
    gethostname (hostName, sizeof (hostName));
    StrPCopy(hostName, Name);
    hostEnt := gethostbyname (hostName);
    if Assigned (hostEnt) then
      if Assigned (hostEnt^.h_addr_list) then begin
        addr := hostEnt^.h_addr_list^;
        if Assigned (addr) then begin
          IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
          byte (addr [1]), byte (addr [2]), byte (addr [3])]);
          Result := True;
        end
        else
          Result := False;
      end
      else
        Result := False
    else begin
      Result := False;
    end;
  finally
    WSACleanup;
  end
end;
procedure TForm1.Button1Click(Sender: TObject);
var IP: string;
begin
 if HostToIp(Edit1.Text, IP) then Label1.Caption := IP
end;

end.


Hope it will help you .
Best Regards Ferhad
kiranramesh:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Hi!
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
https://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: DragonSlayer

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

...Snehanshu
EE Cleanup Volunteer