473,480 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sockets and ReceiveFrom >>> Please Help Rich

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
Nov 15 '05 #1
3 2389
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" <fr**@codesoldier.com> wrote in message
news:Oy****************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #2
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" <no****@harmonyit.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <fr**@codesoldier.com> wrote in message
news:Oy****************@TK2MSFTNGP09.phx.gbl...
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


Nov 15 '05 #3
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" <fr**@codesoldier.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <no****@harmonyit.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <fr**@codesoldier.com> wrote in message
news:Oy****************@TK2MSFTNGP09.phx.gbl...
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



Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
10047
by: Nick Keighley | last post by:
I'm probably missing something rather obvious, but is there a known problem with getting a Python based socket program to communicate with a C based socket program? A simple echo server written in...
15
28915
by: Mattia | last post by:
Hi! I have a <form> that can be submitted thruogh three buttons and I need to tell witch one was pressed when the form was submitted. I now do it with 3 <input type="submit" name="..."...
19
17318
by: CMAR | last post by:
I have the following markup. The problem is that the browser, e.g., IE6, inserts several lines of blank space between the <div> and the following table. Is there a way to minimize that vertical...
10
1653
by: Christopher Benson-Manica | last post by:
Sorry for the lame title, but it's hard to condense what I'm about to say... Here's my situation (my first real C++ situation, yay!). Currently we have a MyTCPThread (don't let the name scare you,...
15
2432
by: kernel.lover | last post by:
Hello, i want to know to have multiple clients connects to same server program does following is correct code #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include...
9
8688
by: Stuart | last post by:
I am trying to execute a Socket.Select() statement on an arraylist of sockets. The problem is that I can only go up to 64 sockets at a time. I know that I have to manipulate the FD_SetSize to...
19
3559
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
19
2112
by: Coward 9 | last post by:
HI, I saw in an example hello.aspx, there is a <form tagbeing used like <form runat="server> I search all html tag references and could NOT find "runat" attributes for <formtag. which...
5
2769
by: matt | last post by:
This is a strange one but I've been stuck on it for days. Any help appreciated. THE PLAN: I've a database that I use a script to grab all the entries for a particular field. I then want to...
0
7033
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6903
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2987
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
170
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.