Hello,
I am no expert just trying to make my final year project work.
I am trying to discover devices on my network and for that use the following code.
private void broadcast()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
IPEndPoint bcastTarget = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 161);
SnmpV2Packet disPacket = new SnmpV2Packet("public");
disPacket.Pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); //System Description OID
byte[] outBuffer = disPacket.encode();
// Send the request
socket.SendTo(outBuffer, (EndPoint)bcastTarget);
IPEndPoint inEP = new IPEndPoint(IPAddress.Any, 0);
byte[] inbuf = new byte[32 * 1024];
DateTime startTime = DateTime.Now;
while ((DateTime.Now - startTime).TotalSeconds < 1)
{
int inlen = 0;
try
{
EndPoint ep = inEP;
inlen = socket.ReceiveFrom(inbuf, ref ep);
inEP = ep as IPEndPoint;
}
catch
{
inlen = -1;
}
if (inlen > 0)
{
if (!inEP.Address.Equals(IPAddress.Broadcast))
{
try
{
disPacket.decode(inbuf, inlen);
Console.WriteLine("Machine Name: " + disPacket.Pdu.VbList[0].Value.ToString() +
"; IP Address: " + inEP.Address.ToString());
}
catch
{
continue;
}
}
}
}
}
I am a newbie so forgive me if this is an obvious mistake. When I try to run my code i get a SocketException which says "A connection attempt failed because the connected party did not prperly respond after a period of time, or established connection failed because conneced host failed to respond". I am obviously not setting up my network right. I have snmp service and snmp trap services running on my laptop. I am conneced to a wireless router which has another pc connected. Please can you point out the mistake?
Thank you