newbie question about object arrays and events

G

Guest

Hi,

I have a class library that I reference in my project. The class library is
a wrapper for the UDP client class. Now when data comes in on a socket, it
notifies my application and I can then retrieve that data. The problem is how
to handle events that might happen at the same time. If you bear with me I
can outline it with some code as follows.

In my application, I have an array of objects that are instances of this
class library.
e.g.
MyClassLib.Sock[] MySock = new MyClassLib.Sock[3]

I instantiate each object as follows::

int intPortToBind = 5600;
for(int intLoopCounter = 0; intLoopCounter < 3; intLoopCounter++)
{
MySock[intLoopCounter] = new MyClassLib.Sock();
MySock[intLoopCounter].Bind(intPortToBind);
MySock[intLoopCounter].Event1 += new
MyClassLib.Sock.Event1Handler(MySock_Event1);
intPortToBind++;
}

And finally my event handler looks like the following::
private void MySock_Event1(string sData, string sRemoteHost, int nRemotePort)
{
tbReceive.Text = tbReceive.Text + sData ;
}

Now I can communicate with peers fine, as long as it is one at a time.
However if I try sending some data to "localhost" through a UDP connection, I
get back data only from one object not all three. I even step through the
debugger, and my event handler fires all the time, and I get valid data from
all three objects, but the text box only has data from one of the objects.

Here's how my send code looks like::

int intPortToBind = 5600;
for(int intLoopCounter = 0; intLoopCounter < 3; intLoopCounter++)
{
MySock[intLoopCounter].Send(intLoopCounter.ToString(), "localhost",
intPortToBind);
intPortToBind++;
}

Theoretically the output text box should have 012, but it sometimes has 1,
or sometimes has 2 etc.

Does it have something to do with the fact that data is coming on all 3
sockets at the same time. If so shouldn't it all be queued up in buffers
associated with all three sockets? Can someone suggest a workaround this, if
they have to run into something similar?

Thanks in advance,
Pete.
 
N

Nicholas Paldino [.NET/C# MVP]

Peter,

There are a few things going on here. First, your event handler doesn't
follow the pattern for event handlers as prescribed. You should have an
event handler method with two parameters, the first being of type object,
the second of a type that is or derives from EventArgs, which has any
information the object itself does not convey.

If you used this pattern, you would be able to tell which object the
event was fired on (you would have to cycle through the array to do a
comparison, but you could find out still).

The last thing is that you are using UDP, which doesn't guarantee packet
delivery or order. If you need something which guarantees this, use a TCP
socket connection.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Krikelis said:
Hi,

I have a class library that I reference in my project. The class library
is
a wrapper for the UDP client class. Now when data comes in on a socket, it
notifies my application and I can then retrieve that data. The problem is
how
to handle events that might happen at the same time. If you bear with me I
can outline it with some code as follows.

In my application, I have an array of objects that are instances of this
class library.
e.g.
MyClassLib.Sock[] MySock = new MyClassLib.Sock[3]

I instantiate each object as follows::

int intPortToBind = 5600;
for(int intLoopCounter = 0; intLoopCounter < 3; intLoopCounter++)
{
MySock[intLoopCounter] = new MyClassLib.Sock();
MySock[intLoopCounter].Bind(intPortToBind);
MySock[intLoopCounter].Event1 += new
MyClassLib.Sock.Event1Handler(MySock_Event1);
intPortToBind++;
}

And finally my event handler looks like the following::
private void MySock_Event1(string sData, string sRemoteHost, int
nRemotePort)
{
tbReceive.Text = tbReceive.Text + sData ;
}

Now I can communicate with peers fine, as long as it is one at a time.
However if I try sending some data to "localhost" through a UDP
connection, I
get back data only from one object not all three. I even step through the
debugger, and my event handler fires all the time, and I get valid data
from
all three objects, but the text box only has data from one of the objects.

Here's how my send code looks like::

int intPortToBind = 5600;
for(int intLoopCounter = 0; intLoopCounter < 3; intLoopCounter++)
{
MySock[intLoopCounter].Send(intLoopCounter.ToString(), "localhost",
intPortToBind);
intPortToBind++;
}

Theoretically the output text box should have 012, but it sometimes has 1,
or sometimes has 2 etc.

Does it have something to do with the fact that data is coming on all 3
sockets at the same time. If so shouldn't it all be queued up in buffers
associated with all three sockets? Can someone suggest a workaround this,
if
they have to run into something similar?

Thanks in advance,
Pete.
 

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