none
Client-server data sending RRS feed

  • Question

  • Hello everyone,

    I have a client-server application that has been causing me some trouble lately. I have found out that if I send requests from the client too fast, the server just reads it all as one request and as you can imagine that throws a deserialization exception.

    What is the best way to serialize objects of a custom type into a network stream at any speed and have the server always read those one by one without combining any of their corresponding byte arrays in the stream?

    Regards,

    suzi9spal

    Tuesday, May 12, 2015 10:21 AM

Answers

  • Hello,

    How do you read incoming requests on the server side? If you use the tcp protocol and send two packages, it is not guaranteed that you receive two packages too. The tcp protocol abstracts from packages and provides a method to exchange byte streams: Each byte sequence you sent is received in the same order without any missing or additional bytes. If you want to transport distinct messages over a tcp byte stream, you must encode them in a way so that the receiver knows when a message starts and ends. You could achieve this by either terminating all messages with a unique character or by prepending the size of the message to the message itself.

    However, it seems like you should use http for that as https provides distinct requests and responses. Even more, you get a lot of features for free like security, routing, proxying or easy inspection by various tools like fiddler.

    Regards,
    Henning Dieterichs
    Developer-Hotline for MSDN Online Germany

    Disclaimer:
    Please take into consideration, that further inquiries cannot or will be answered with delay.
    For further information please contact us per telephone through the MSDN-Entwickler-Hotline: http://www.msdn-online.de/Hotline
    MSDN-Entwickler-Hotline: Fast and professional help for software developers free of charge!

    For this post by the MSDN-Entwickler-Hotline the following terms and conditions apply: Trademarks, Privacy as well as the separate terms of use for the MSDN-Entwickler-Hotline .

    • Marked as answer by suzi9spal Wednesday, May 13, 2015 5:55 PM
    Tuesday, May 12, 2015 12:42 PM

All replies

  • Hello,

    How do you read incoming requests on the server side? If you use the tcp protocol and send two packages, it is not guaranteed that you receive two packages too. The tcp protocol abstracts from packages and provides a method to exchange byte streams: Each byte sequence you sent is received in the same order without any missing or additional bytes. If you want to transport distinct messages over a tcp byte stream, you must encode them in a way so that the receiver knows when a message starts and ends. You could achieve this by either terminating all messages with a unique character or by prepending the size of the message to the message itself.

    However, it seems like you should use http for that as https provides distinct requests and responses. Even more, you get a lot of features for free like security, routing, proxying or easy inspection by various tools like fiddler.

    Regards,
    Henning Dieterichs
    Developer-Hotline for MSDN Online Germany

    Disclaimer:
    Please take into consideration, that further inquiries cannot or will be answered with delay.
    For further information please contact us per telephone through the MSDN-Entwickler-Hotline: http://www.msdn-online.de/Hotline
    MSDN-Entwickler-Hotline: Fast and professional help for software developers free of charge!

    For this post by the MSDN-Entwickler-Hotline the following terms and conditions apply: Trademarks, Privacy as well as the separate terms of use for the MSDN-Entwickler-Hotline .

    • Marked as answer by suzi9spal Wednesday, May 13, 2015 5:55 PM
    Tuesday, May 12, 2015 12:42 PM
  • Hello,

    I am aware of the TCP offering sending of unchanged byte arrays, but that has not proved reliable.

    I used the prepending the size of the message to the message itself approach, but it sometimes throws deserialzation exceptions. For example: I run the application and it throws the exception, closing the program, or it successfully deserializes the data in the stream without any problems (even though restarting the program changes nothing code-wise). I also tested my code many times, to see if I'm somehow calculating the length of the message incorrectly, but I can't find an issue. The exception thrown is summed up to "0 is not a valid binary header". Which doesn't say that much except that the byte array I'm trying to deserialize has been currupted in a way(for example cutting off the last byte). I have mentioned this serialization problem in another post HERE. Though I created another thread, searching for alternative ways.

    Any help would be greatly appreciated,

    suzi9spal


    • Edited by suzi9spal Tuesday, May 12, 2015 4:34 PM Grammar
    Tuesday, May 12, 2015 4:31 PM
  • Hi Suzi,

    If you are prepending the size of the message, then you should be using that size to guarantee that you've received the entire message. Since it's throwing exceptions occasionally when deserializing, I suspect that you haven't checked the size of the message before deserializing?

    You should be reading the data from the port into a buffer and grabbing X bytes of it out of that buffer to deserialize (X being the prepended size of the message). Maybe you could show us some code?


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, May 13, 2015 2:35 PM
    Moderator
  • Hello Bonnie,

    You can find the code in another thread I posted (HERE). I made this new thread only to find out the best way to send multiple serialized object over a stream. I have been prepending the size of the message to the message itsself until now, with occasional exceptions. You can read more about that on that link.

    Thank you for your help,

    suzi9spal

    Wednesday, May 13, 2015 5:55 PM