Solved

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

Posted on 2002-07-25
14
286 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:kiranramesh
  • 4
  • 3
  • 2
  • +5
14 Comments
 

Expert Comment

by:Akazukin
ID: 7176729
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
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7176752
Check out the API function NetServerEnum();

HTH
DragonSlayer
0
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 7176788
listening...
0
 

Author Comment

by:kiranramesh
ID: 7176848
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
 

Author Comment

by:kiranramesh
ID: 7176880
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
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7177043
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
 

Author Comment

by:kiranramesh
ID: 7177366
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
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!

 
LVL 2

Expert Comment

by:mullet_attack
ID: 7178527
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
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 100 total points
ID: 7179221
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
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7179224
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
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7179249
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
 
LVL 2

Expert Comment

by:ferhad
ID: 7211920
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
 

Expert Comment

by:CleanupPing
ID: 9342998
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 9965804
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

20 Experts available now in Live!

Get 1:1 Help Now