Search code examples
stringbinaryclient-serverpackettcp

Is there an advantage of sending binary packets over strings?


The title says it all. Basically I'm using TCP for a client-server setup and I'm wondering if there is an advantage of transforming strings to binary before sending the data over tcp?


Solution

  • Strings are binary data, or can at least be easily converted to such, byte[], with

    static byte[] GetStringBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }
    

    If you compress/encode the data you send, whether it starts out life as a string or binary data you will most likely be sending the same total number of bytes.