newbie question about object arrays and events

Discussion in 'Microsoft C# .NET' started by Guest, Dec 2, 2004.

  1. Guest

    Guest 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.
     
    Guest, Dec 2, 2004
    #1
    1. Advertisements

  2. 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]
    -

    "Peter Krikelis" <> wrote in message
    news:...
    > 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.
     
    Nicholas Paldino [.NET/C# MVP], Dec 2, 2004
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nikhil Patel

    Rectangular arrays Vs. multidimensional arrays.

    Nikhil Patel, Jul 20, 2003, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    412
    Peter N Roth
    Jul 21, 2003
  2. Guest

    Question about events and object arrays

    Guest, Dec 7, 2004, in forum: Microsoft C# .NET
    Replies:
    9
    Views:
    194
    Guest
    Dec 7, 2004
  3. Arrays of Multidimensional Arrays?

    , Mar 21, 2006, in forum: Microsoft C# .NET
    Replies:
    9
    Views:
    287
    Lars-Inge T√łnnessen \(VJ# MVP\)
    Mar 21, 2006
  4. Arrays of Multidimensional Arrays?

    , Mar 21, 2006, in forum: Microsoft C# .NET
    Replies:
    0
    Views:
    237
  5. Daniel
    Replies:
    3
    Views:
    511
    Bruce Wood
    Feb 1, 2007
Loading...

Share This Page