Sockets and ReceiveFrom >>> Please Help Rich

F

Fred Palmer

I am trying to get reponses from multiple devices on my network by sending the
following message via UDP:

// Create the message
Byte[] bytesSent = new Byte [] {0x00, 0x00, 0x00, 0xF6};

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
// Send the message to be broadcast
s.SendTo(bytesSent, bytesSent.Length, 0, new
IPEndPoint(IPAddress.Parse("192.168.1.255"), 30718));

IPEndPoint senderEndPoint = new IPEndPoint(IPAddress.Any, 0);
EndPoint senderRemote = (EndPoint)senderEndPoint;

byte[] msg = new Byte[256];
// Gets the message from the first device to respond.
s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, ref senderRemote);
textBox1.AppendText(senderRemote.ToString() + "\r\n");
s.Close();

Currently this works, but how do I get messages from multiple devices on my
network? The message gets to all of them and they all send a response, but I
just can't figure out how to process them. Essentially all of these devices are
returning an acknowledgement that they exist, so I just need to get their IP
address as in the example.

Thanks,
Fred
 
T

Tom Hall

You've got the right idea, using the ReceiveFrom that takes a ref to an
EndPoint initialized to (IPAddress.Any,0).

My code for this looks like:

EndPoint rep=new IPEndPoint(IPAddress.Any,0);
int cnt=udpSocket.ReceiveFrom(udpBuffer,ref rep);
string remoteAddress=(((IPEndPoint)rep).Address.ToString();

Voila - you can use the "xx.xx.xx.xx" address now in remoteAddress to
determine which unit sent you the data.

Note that you can't use and track the EndPoint as you are creating a new one
everytime so the references are different and if you store them in a
Hashtable or something you might never find them again using a newly created
one - the HashCode value is based on the Address and the Port (which could
change on every send from a remote device) - found that one out the hard
way - ouch!

HTH
Tom
 
F

Fred Palmer

Tom,

Thanks for the response. To better explain my situation, how would I get the
messages from multiple devices that are responding to my broadcast? The example
I posted only gets a response from one device. I know there are at least 5
devices responding ( I used a packet sniffer). My current implementation only
gets the first response to come to my machine.

Thanks,
Fred

Tom Hall said:
You've got the right idea, using the ReceiveFrom that takes a ref to an
EndPoint initialized to (IPAddress.Any,0).

My code for this looks like:

EndPoint rep=new IPEndPoint(IPAddress.Any,0);
int cnt=udpSocket.ReceiveFrom(udpBuffer,ref rep);
string remoteAddress=(((IPEndPoint)rep).Address.ToString();

Voila - you can use the "xx.xx.xx.xx" address now in remoteAddress to
determine which unit sent you the data.

Note that you can't use and track the EndPoint as you are creating a new one
everytime so the references are different and if you store them in a
Hashtable or something you might never find them again using a newly created
one - the HashCode value is based on the Address and the Port (which could
change on every send from a remote device) - found that one out the hard
way - ouch!

HTH
Tom


Fred Palmer said:
I am trying to get reponses from multiple devices on my network by sending the
following message via UDP:

// Create the message
Byte[] bytesSent = new Byte [] {0x00, 0x00, 0x00, 0xF6};

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
// Send the message to be broadcast
s.SendTo(bytesSent, bytesSent.Length, 0, new
IPEndPoint(IPAddress.Parse("192.168.1.255"), 30718));

IPEndPoint senderEndPoint = new IPEndPoint(IPAddress.Any, 0);
EndPoint senderRemote = (EndPoint)senderEndPoint;

byte[] msg = new Byte[256];
// Gets the message from the first device to respond.
s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, ref senderRemote);
textBox1.AppendText(senderRemote.ToString() + "\r\n");
s.Close();

Currently this works, but how do I get messages from multiple devices on my
network? The message gets to all of them and they all send a response, but I
just can't figure out how to process them. Essentially all of these devices are
returning an acknowledgement that they exist, so I just need to get their IP
address as in the example.

Thanks,
Fred
 
T

Tom Hall

If you will always broadcast and don't really care who answers but you want
to identify all responses you need to keep calling ReceiveFrom. The easiest
way is to start a separate thread to send the requests and have another
thread always waiting on ReceiveFrom. That's it. How you structure this
depends on your ultimate application.

e..g
Loop
ReceiveFrom
Print
EndLoop


If you are doing a request/response type of thing where you need to keep
track of state, you need to use a Hashtable or other list to track the
devices (by IP address) and their current state and dispatch the appropriate
code based on the state when a response arrives. You can also get into
complicated scenarios where you have a timer checking the Hashtable for
items whose state is "stuck" - like a request sent and no response by
timeout and maybe retry them - depends on your application.

Also of note: UDP is not dependable at several levels. Your request may
get lost, the device response may get lost and never arrive. As well, on
Windows (at least), every 10 minutes or so the IP Stack will ARP to check
that the IP Address still corresponds to the ethernet MAC address. If you
are continuously transmitting packets (and are unlucky - like me), you will
try to transmit 2 packets while the stack is waiting for the ARP response
from the device - the result (on XP) is that only the last packet queued
will get transmitted! The first one(s) will be silently dropped! Causes
unpredictable havoc if you expect all packets will arrive.

This may not apply to your situation as you are using the broadcast address
BUT it will apply if you ever transmit directly to the units that have
responded to your broadcast. I assume you are broadcasting to learn all the
devices on the local network and then will start communicating individually
with them.

HTH
Tom



Fred Palmer said:
Tom,

Thanks for the response. To better explain my situation, how would I get the
messages from multiple devices that are responding to my broadcast? The example
I posted only gets a response from one device. I know there are at least 5
devices responding ( I used a packet sniffer). My current implementation only
gets the first response to come to my machine.

Thanks,
Fred

Tom Hall said:
You've got the right idea, using the ReceiveFrom that takes a ref to an
EndPoint initialized to (IPAddress.Any,0).

My code for this looks like:

EndPoint rep=new IPEndPoint(IPAddress.Any,0);
int cnt=udpSocket.ReceiveFrom(udpBuffer,ref rep);
string remoteAddress=(((IPEndPoint)rep).Address.ToString();

Voila - you can use the "xx.xx.xx.xx" address now in remoteAddress to
determine which unit sent you the data.

Note that you can't use and track the EndPoint as you are creating a new one
everytime so the references are different and if you store them in a
Hashtable or something you might never find them again using a newly created
one - the HashCode value is based on the Address and the Port (which could
change on every send from a remote device) - found that one out the hard
way - ouch!

HTH
Tom


Fred Palmer said:
I am trying to get reponses from multiple devices on my network by
sending
the
following message via UDP:

// Create the message
Byte[] bytesSent = new Byte [] {0x00, 0x00, 0x00, 0xF6};

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
// Send the message to be broadcast
s.SendTo(bytesSent, bytesSent.Length, 0, new
IPEndPoint(IPAddress.Parse("192.168.1.255"), 30718));

IPEndPoint senderEndPoint = new IPEndPoint(IPAddress.Any, 0);
EndPoint senderRemote = (EndPoint)senderEndPoint;

byte[] msg = new Byte[256];
// Gets the message from the first device to respond.
s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, ref senderRemote);
textBox1.AppendText(senderRemote.ToString() + "\r\n");
s.Close();

Currently this works, but how do I get messages from multiple devices
on
my
network? The message gets to all of them and they all send a
response,
but I
just can't figure out how to process them. Essentially all of these devices are
returning an acknowledgement that they exist, so I just need to get
their
IP
address as in the example.

Thanks,
Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top