PC Review


Reply
Thread Tools Rate Thread

newbie question about object arrays and events

 
 
=?Utf-8?B?UGV0ZXIgS3Jpa2VsaXM=?=
Guest
Posts: n/a
 
      2nd Dec 2004
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.
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      2nd Dec 2004
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 Removed)

"Peter Krikelis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
events that fire events that fire events....a bad thing? Daniel Microsoft C# .NET 3 1st Feb 2007 02:55 AM
Arrays - declaration, adding values to arrays and calculation Maxi Microsoft Excel Programming 1 17th Aug 2006 04:13 PM
Question about events and object arrays =?Utf-8?B?UGV0ZXIgS3Jpa2VsaXM=?= Microsoft C# .NET 9 7th Dec 2004 08:51 PM
arrays in arrays jacob Microsoft Excel Programming 4 29th Aug 2004 08:40 PM
Rectangular arrays Vs. multidimensional arrays. Nikhil Patel Microsoft C# .NET 2 21st Jul 2003 02:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:10 PM.