locked
Problem with SocketAsyncEventArgs.Completed event - expert question RRS feed

  • Question

  • Hello all,

    I wrote a simple silverlight network application sending data asynchronously using Socket.SendAsync in a classical scenario.

    Ocassionally, when the SocketAsyncEventArgs.Completed event is rised, SocketAsyncEventArgs.BytesTransferred is less than SocketAsyncEventArgs.Count.

    Completed event fires when data in buffer is sent only partially. I am absolutely certain, since all the packets set in buffer have always the same, known length (SocketAsyncEventArgs.Count = const).

    I noticed this problem only on Mac OS X using WAN networks.

    I guess there is a reason why SocketAsyncEventArgs exposes separate BytesTransferred and Count properties, but I found no example of how to correctly verify that entire buffer is sent and what to do when it is not. All simplified examples available on MSDN assume that Completed event is rised when entire buffer is sent but SendAsync() method returns true - which may not always be the case.

    I will appreciate any pointers/hints. Socket synchronous methods are not available in silverlight and blocking sending thread till Completed event fires does not help.


    Friday, January 11, 2013 11:45 PM

Answers

  • I spent many hours investigating this issue. It seems that it is just not guaranteed that the Completed event fires when the async send operation is really completed.
    Typically it fires just once, even whith large packets - I used 30k for tests. Unfortunately MSDN does not even mention that.
    I found some examples on how to handle such situation on Chinese discussion groups (simply SendAsync again whatever is remaining).
    Thanks, brothers in pain ;)
    Monday, January 14, 2013 9:59 PM

All replies

  •  

    This is normal for any communication path that doesn't have a built in end-of-data method.  To ensure you get all the data you need to do one of three things

    1)  Make each message a fixed number of bytes.  the at the recieve end read the known number of bytes.  for example of you are sending a command 'RUN' the receive end knows that 'RUN' is three bytes.

    2) Add a byte count to the transmit message at the beginning of the message.  for example

    45The quick Brown Fox Jumped over the lazy dog.

    3) Add a Return to the end of each transmit message and then read until you get the return

    The quick Brown Fox Jumped over the lazy dog.\n

     


    jdweng

    Saturday, January 12, 2013 12:15 PM
  • Joel, I am afraid you do not entirely understand the problem.
    What you said is all true but it mainly applies to receiving while the problem is sending.
    Briefly, while sending the
    Completed event fires when it should not.

    Saturday, January 12, 2013 5:02 PM
  • Hi Tomasz,

    Welcome to the MSDN Forum.

    Please try this issue on silverlight: http://social.msdn.microsoft.com/Forums/en-us/category/silverlight

    forum or System.net forum: http://social.msdn.microsoft.com/Forums/en/wcf/threads.

    Thank you.


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 14, 2013 10:20 AM
  • I've been havving issues submitting responses on my computer at home.  I think there is a certifate issues but not sure.  Sorry for the late response. 

    TCP (IP packet) datagrams are around 1500 bytes.  When sending Asyn with byte counts greater than 1500 you will get mutiple Events.  I'm not sure what happens for the 1st datagram if you may get an extra Event.  I would also check the tx byte[] to make sure you aren't sending a data with a byte count of zero.

     

    It is poosible the client has the property keepalive set to true which means the client will periodically send a test message with no data to verify the connection is active.  When the low level socket responds to the keep alive an extra event may be occuring.

     


    jdweng

    Monday, January 14, 2013 10:35 AM
  • I spent many hours investigating this issue. It seems that it is just not guaranteed that the Completed event fires when the async send operation is really completed.
    Typically it fires just once, even whith large packets - I used 30k for tests. Unfortunately MSDN does not even mention that.
    I found some examples on how to handle such situation on Chinese discussion groups (simply SendAsync again whatever is remaining).
    Thanks, brothers in pain ;)
    Monday, January 14, 2013 9:59 PM
  • Hi Tomasz,

    Thank you for sharing your solution.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 15, 2013 1:55 AM