dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Binary vs. Textual Protocol

  1. #1
    Join Date
    Dec 2004
    Posts
    293

    Question Binary vs. Textual Protocol

    Hi.

    I need to design a new protocol that "rides" on TCP/IP and I wonder what kind of protocol is better - binary or textual ??
    in terms of size, easier to debug, flexible, scalalble and other virtues you might think of...

    I know that textual is much easier to debug, but tends to require bigger data size than binary....what other advantages/disadvantages exist when designing a new protocol ??

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Binary vs. Textual Protocol

    Speed of encoding/decoding might be a consideration. Human-readable text is slower to encode and decode than binary.

    A big consideration is how much data is ususally going to be sent. If you're only sending 1k or 2k then a textual protocol is probably going to be fine.

    If you're sinding 1Mb or more then binary would generally be the most efficient because of the speed of encoding/decoding.

    It might be worthwhile supporting both - you might consider sending a byte indicating whether the data is text or binary. This way when debugging you can send text and read it. When you're ready to put your code into production then just use binary.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Apr 2007
    Location
    Ireland
    Posts
    81

    Re: Binary vs. Textual Protocol

    Textual is probably easier to work with and debug but as you say may result in larger data sizes.

    Binary may give you smaller data sizes buy you need to take special care that you don't run into cross-platform issues such as different word sizes or different endianness across different operating systems etc. If you do you use binary then a good rule of thumb is to define the protocol in terms of bytes - this should help avoid most cross platform issues.

  4. #4
    Join Date
    Dec 2004
    Posts
    293

    Re: Binary vs. Textual Protocol

    I thought about using textual protocol between client and server in this format:

    Code:
    start line\r\n 
    message-header-1: header value 1\r\n 
    message-header-2: header value 2\r\n 
    ... 
    message-header-n: header value n\r\n 
    \r\n 
    message body of zero or more bytes 
    NULL (\0)
    and between different servers to use binary protocol.

    is it reasonable ??

    Thanks

  5. #5
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Binary vs. Textual Protocol

    You could have both protocols as layers over TCP/IP:

    Text protocol
    -----------------------------
    Binary protocol
    -----------------------------
    TCP/IP
    Har Har

  6. #6
    Join Date
    Dec 2004
    Posts
    293

    Re: Binary vs. Textual Protocol

    I don't follow you, can you give a specific example ??

    why having a textual version over binary ?? it just add complicity - no??

  7. #7
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Binary vs. Textual Protocol

    Quote Originally Posted by Shvalb
    I don't follow you, can you give a specific example ??

    why having a textual version over binary ?? it just add complicity - no??
    It adds complexity but also flexibility. You can choose what protocol to use and when, depending on what your current data ( type and size). And you can combine the two of them to have the ease of use of the text protocol and the performance of the binary one. Ex: you pack your data using the text protocol then pack it using the binary protocol ( maybe compress it) and send it overt TCP/IP.
    Har Har

  8. #8
    Join Date
    Dec 2004
    Posts
    293

    Re: Binary vs. Textual Protocol

    That sounds like a nice idea....I'll try to think it over and see if I can come up with a nice interface to it.

    Can you provide a simple interface to this kind of protocol ??

    Thanks!

  9. #9
    Join Date
    Dec 2004
    Posts
    293

    Re: Binary vs. Textual Protocol

    Question about parsing textual msg:

    suppose I have a method that receives a buffer that is a textual message and it needs to parse it.

    1. is it reasonable to create a std:string and use it to parse the data ??
    2. is it reasonable in the parsing to check that every field has the correct name and if not to throw an error or it's reasonable to assume that all fields have the correct name and order ??

    example for msg:

    Code:
    Encode: YES
    Version: 2
    Type: InitRequest
    From: 03847534
    To: 0
    BodySize: 35
    
    Email=oval@gmail.com&Password=03609&
    meaning, to read the first 6 bytes and check that it really says "Encode" and if it does than read its value and if not --> return error and so on.....

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)