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.
kiranrameshAsked:
Who is Participating?
 
DragonSlayerConnect With a Mentor Commented:
Okay try this:

unit uMain;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    lblTotal: TLabel;
    lblRead: TLabel;
    ListView1: TListView;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  LM, WinSock;

type
  TServerInfoArray = array [1 ..
    ($1FFFFFFF div SizeOf(TServerInfo101))] of TServerInfo101;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  TotalEntries, Entries: Cardinal;
  Info: Pointer;
  ListItem: TListItem;
  i: Integer;
begin
  NetServerEnum(nil, 101, Info, $FFFFFFFF,
    Entries, TotalEntries, SV_TYPE_ALL, nil, nil);
  lblTotal.Caption := IntToStr(TotalEntries);
  lblRead.Caption := IntToStr(Entries);

  ListView1.Items.BeginUpdate;
  try
    ListView1.Items.Clear;
    for i := 1 to TotalEntries do
      with TServerInfoArray(Info^)[i] do
      begin
        ListItem := ListView1.Items.Add;
        ListItem.Caption := sv101_name;
        ListItem.SubItems.Add(sv101_comment);
      end;
  finally
    ListView1.Items.EndUpdate;
  end;
end;

end.
0
 
AkazukinCommented:
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;

0
 
DragonSlayerCommented:
Check out the API function NetServerEnum();

HTH
DragonSlayer
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
gandalf_the_whiteCommented:
listening...
0
 
kiranrameshAuthor Commented:
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.
0
 
kiranrameshAuthor Commented:
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.
0
 
mullet_attackCommented:
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
0
 
kiranrameshAuthor Commented:
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.
0
 
mullet_attackCommented:
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
0
 
DragonSlayerCommented:
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
0
 
DragonSlayerCommented:
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.
0
 
ferhadCommented:
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
0
 
CleanupPingCommented:
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.
0
 
snehanshuCommented:
Hi!
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://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
0
All Courses

From novice to tech pro — start learning today.