polling an intranet for a device type

  • Thread starter Thread starter tony@avonwood
  • Start date Start date
T

tony@avonwood

Hi,

I am looking to poll our internal network for network devices that could
have any IP Address.

Assuming I know the IP range these devices will be within, can someone point
me to a suitable tutorial where I can learn how to poll the IP Range, and
also query any device found for compatibility.

TIA

Tony
 
Hi Peter,

Many thanks for your response.

We have some new products that are network visible and can have any IP
address available. They respond to Pings, and by compatibility I mean that
they will interpret and respond to specific query's with predefined answers

I need to be able to detect those devices by means of sending either a
single global (network) command and store the IP Addresses of the devices
that respond in the predefined manner, eg. I ask "Are you my new device" and
the response is "Yes I am" (very simplistic).
Alternatively I could query each individual IP address and monitor the
response from that IP Address. If I were to ping each address I would
effectively be doubling the query time as PC's and other network devices,
e.g. printers etc, would also respond to a ping and would then require
additional querying again to determine the device type. Our devices are
programmed to respond to various commands in a predefined way, eg I send a
'?' and the device responds with a firmware version.

Is that a bit more substantial.

TIA

Tony
 
Hi,

I am looking to poll our internal network for network devices that could
have any IP Address.

Assuming I know the IP range these devices will be within, can someone
point me to a suitable tutorial where I can learn how to poll the IP
Range, and also query any device found for compatibility.


I have been thinking about this, I haven't written the code yet but
something like:

for( i = 1; i < 255; i++ )
{
IPaddress = string.format("192.168.1.{0}", i )
connect to your specific port, eg ssh
if( connect ok )
list.add( IPaddress );
disconnect.
}

Note: I am in a known network, I could also check my machine settings for
actual IP address and netmask as an improvement.

Note2: There are other reasons why connection fails, I am interested in
'available' devices not 'potentially available'.

Note that doing this has been known to crash specific devices. It
shouldn't but it did! (I have had AIX servers crash on a network
security probe)

Ken
 
Hi,

You've probably guessed from posts that I'm new to Socket programming, hence
my posts being lacking in the content you require.

I'm looking to collate a list of specific devices on a network by sending a
network message which our network devices will respond to, but non-specific
devices (in this case PC's, printers etc) will ignore.
I'm needing to send this command from my PC (or whichever PC is running the
application) across a local intranet. I am not sure whether it is better to
use TCPIP or UDP. Once our devices have responded I am then able to collate
a list of addresses for our devices and then use that to communicate with a
specific chosen device from that list.

I have succeeded in communicating 1 to 1 with a known IP address, but I am
struggling to to collate a list of our devices connected to my network.
Our devices are capable of UDP and TCP.

I'm thinking I will need to use UDP for a network broadcast, and TCP for an
individual socket connection?

Thanks for your hlp so far.

Peter Duniho said:
[...]
I have been thinking about this, I haven't written the code yet but
something like:

for( i = 1; i < 255; i++ )
{
IPaddress = string.format("192.168.1.{0}", i )
connect to your specific port, eg ssh
if( connect ok )
list.add( IPaddress );
disconnect.
}

As far as pseudo-code goes, I guess that's fine. However, note that the
above code will be extremely slow on a sparsely populated network, because
every connection attempt that fails won't do so until a several-second
timeout occurs.

The OP hasn't provided any more details about his problem. Not about what
protocols the devices support or respond to, nor about what level of
network programming experience he has, or even the exact nature of his
question. Speaking hypothetically though, the ideal situation would be
network devices that respond in a unique, well-defined way to a UDP
broadcast. Then the querying computer could just broadcast a message
asking for relevant devices to respond. Optimizations to the protocol
could be made to deal with network congestion issues, if having every
device respond to a single query results in some of those responses being
lost.

If indeed a TCP connection to each device is required, then rather than a
serialized enumeration of each IP address, I would use the asynchronous
pattern for the Socket class (or TcpClient, according to one's
preference), so that failed connections don't slow down detection of the
successful ones. It would require up to 254 sockets to exist
simultaneously, which is not a problem at all.

But, who knows whether any of this is useful to the OP at all. Unless he
comes back and continues the thread, we're just shooting in the dark.

Pete
 
Hi,

I've found some VB code that achieves what I want to do, but i'm unsure how
to recreate it.
The form has a UDP component, with a reomte host of 255.255.255.255
assigned, and an event to handle a UDP data arrival.
The UDP then does a network send and all devices are pciked up from the
event handler.

What event would you associate with the UDP?

I have to disappear now before we get snowed but I can provide more detailed
data later.

TIA

Tony
tony@avonwood said:
Hi,

You've probably guessed from posts that I'm new to Socket programming,
hence my posts being lacking in the content you require.

I'm looking to collate a list of specific devices on a network by sending
a network message which our network devices will respond to, but
non-specific devices (in this case PC's, printers etc) will ignore.
I'm needing to send this command from my PC (or whichever PC is running
the application) across a local intranet. I am not sure whether it is
better to use TCPIP or UDP. Once our devices have responded I am then able
to collate a list of addresses for our devices and then use that to
communicate with a specific chosen device from that list.

I have succeeded in communicating 1 to 1 with a known IP address, but I am
struggling to to collate a list of our devices connected to my network.
Our devices are capable of UDP and TCP.

I'm thinking I will need to use UDP for a network broadcast, and TCP for
an individual socket connection?

Thanks for your hlp so far.

Peter Duniho said:
[...]
I have been thinking about this, I haven't written the code yet but
something like:

for( i = 1; i < 255; i++ )
{
IPaddress = string.format("192.168.1.{0}", i )
connect to your specific port, eg ssh
if( connect ok )
list.add( IPaddress );
disconnect.
}

As far as pseudo-code goes, I guess that's fine. However, note that the
above code will be extremely slow on a sparsely populated network,
because every connection attempt that fails won't do so until a
several-second timeout occurs.

The OP hasn't provided any more details about his problem. Not about
what protocols the devices support or respond to, nor about what level of
network programming experience he has, or even the exact nature of his
question. Speaking hypothetically though, the ideal situation would be
network devices that respond in a unique, well-defined way to a UDP
broadcast. Then the querying computer could just broadcast a message
asking for relevant devices to respond. Optimizations to the protocol
could be made to deal with network congestion issues, if having every
device respond to a single query results in some of those responses being
lost.

If indeed a TCP connection to each device is required, then rather than a
serialized enumeration of each IP address, I would use the asynchronous
pattern for the Socket class (or TcpClient, according to one's
preference), so that failed connections don't slow down detection of the
successful ones. It would require up to 254 sockets to exist
simultaneously, which is not a problem at all.

But, who knows whether any of this is useful to the OP at all. Unless he
comes back and continues the thread, we're just shooting in the dark.

Pete
 
Hi Peter,

Thanks for your continued patience.

Our devices are running firmware where they are effectively slaves. they
will respond to specific custom defined commands (see in the code below, as
well as other custim predefined commands). They can provide details of IP,
device, hardware that they are connected to etc. Configuration from the PC
is minimal. Once I have managed to compile a list of IP addresses for
connected devices, I can use Sockets etc to obtain what I want from a
specific device(this is already working when I know the specific IP
Address).

In the sample code I found (which is a legacy project developed here, but by
someone no longer here) the VB app has a form. A component on that form is
of type AxMSWinsockLib.AxWinsock. This component has a protocol of
sckUDPProtocol, with a remotehost assigned to use IPAddress 255.255.255.255,
on port 30718. This component also has an event associated with DataArrival
(although I see you suggest we use BeginReceive() in this instance).

When sending a network broadcast the following code is used
/*start of code

Dim data() As Byte = {0, 0, 0, &HF8} //this asks listening devices of our
type to respond with their IP address
UDP.RemoteHost = "255.255.255.255" 'The program searches for devices on the
whole network via broadcast
UDP.SendData(data)
*/end of code

And then the received data is extracted in the DataArrival function, as
follows

/*start of code
Dim data As String = ""
UDP.GetData(data)
*/end of code

I have tried to recreate this with the following code

/* start of code

int port = 30718;
IPAddress address = 10.0.0.159; //this is example of the address,

IPEndPoint endpoint = new IPEndPoint(address, port);

Byte[] tByteGet = { 0, 0, 0, 0xF8 };
UdpClient UDP = new UdpClient(endpoint);
UDP.Connect(endpoint);

UdpState s = new UdpState();
s.e = endpoint;
s.u = UDP;

UDP.Send(tByteGet, tByteGet.Length);
UDP.BeginReceive(new AsyncCallback(ReceiveCallback), s);

while (false == messageReceived)
{
Thread.Sleep(100);
}
*/ end of code

And the asynchronous handler function is as follows

/*start code

UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;

Byte[] receiveBytes = u.EndReceive(ar, ref e);

m_strNetDeviceRead = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("Received: {0}", m_strNetDeviceRead);
messageReceived = true;
*/ end of code

I hope that is of more use to you.

TIA

Tony
 
Hi Peter,

I think I may have cracked the network broadcast, maybe you could tell me if
the following code is correct

/* Start Code
int nIPPort = 8080;
IPEndPoint endpoint = new IPEndPoint(IPAddress.Broadcast, nIPPort);

try
{
Byte[] tByteGetIP = { 0, 0, 0, 0xF8 };
UdpClient UDP = new UdpClient();
UdpState s = new UdpState();
s.e = endpoint;
s.u = UDP;


UDP.EnableBroadcast = true;
int nBYtes = UDP.Send(tByteGetIP, tByteGetIP.Length, endpoint);

Thread.Sleep(100);

UDP.BeginReceive(new AsyncCallback(ReceiveCallback), s);

while (false == messageReceived)
{
Thread.Sleep(100);
}
}
catch(SocketException e)
{
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}

*/ End Code

To handle the receive I have the following code

/* Start Code
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;

Byte[] Bytes = u.EndReceive(ar, ref e);

strNetDeviceRead = Bytes [4].ToString() + "." + Bytes [5].ToString() + "." +
Bytes [6].ToString() + "." + Bytes [7].ToString(); //Get the IP address

Console.WriteLine("Received: {0}", strNetDeviceRead);


messageReceived = true;

*/ End Code

This seems to be working a treat.

Thanks again

Tony
 
Hi Peter,

After I posted yesterday I modified my ReceiveCallback function to handle
multiple devices by issuing another BeginReceive at the end of the
ReceiveCallback function. This is working for the number of devices I have
on the network My only issue here is determining when there is no more data
to receive, i.e. all the devices have replied.
I have stuck with UDP on the grounds that what I have is working and there
will never be great amounts of devices on a network (nominally 2 or 3) so I
dont foresee any issues.

Everything else you recommend I have implemented (except making it
synchronous) and all is working as I would hope for.

Many many thnaks for your time and patience.

Tony
 
Back
Top